ブール変数ごとに1つのブール値を保存する代わりに、ブール値の配列を表す BoolArray クラスを作成しようとしています。1 つの char 変数を使用して、ビットを使用して 8 つのブール値を表し、メモリを節約します。演算子[]を使って通常の配列として便利にしたい。
arr[5] を使用して、5 番目のビットに bool 値を返すことができます。私の問題はビットの割り当てです。1 つのビットを参照することはできないので、そのタスクを実行する関数を作成する必要があります。私の質問は、演算子のオーバーロードを使用して配列スタイルの割り当てを制御できるかどうかです。私の解決策は、別のクラス (MyBoolean) を使用して、それを BoolArray operator[] への参照として返すことでした。MyBoolean は operator= をオーバーロードし、BoolArray のビットを変更します。operator[]= のようなものをオーバーロードする簡単な方法はありますか?
#include <iostream>
#include <tgmath.h>
using namespace std;
string int_to_binary_string(int number)
{
if ( number == 0 ) return "0";
if ( number == 1 ) return "1";
if ( number % 2 == 0 )
return int_to_binary_string(number / 2) + "0";
else
return int_to_binary_string(number / 2) + "1";
}
class MyBoolean;
class BoolArray {
private:
unsigned char* arr;
int size;
void setBit(int bit_number,bool value);
public:
explicit BoolArray(int size);
~BoolArray();
MyBoolean operator[](unsigned int index);
int getSize() const;
friend class MyBoolean;
};
// ***************************** MyBoolean *********************************
class MyBoolean {
private:
bool value;
int bit_number;
BoolArray* bool_array;
public:
// --- Constructor ---
MyBoolean(bool value,int bit_number,BoolArray* bool_array) :
value(value),bit_number(bit_number),bool_array(bool_array)
{}
// --- cast ---
operator bool() const{
return value;
}
MyBoolean& operator=(bool new_value) {
value = new_value;
bool_array->setBit(bit_number,new_value);
}
};
// ***************************** BoolArray *****************************
void BoolArray::setBit(int bit_number,bool value) {
int index = floor((double)bit_number/8);
bit_number -= index*8;
cout << "trying to set " << bit_number << " to " << value << endl;
if(value==true) {
int binary_num = pow(2,bit_number);
cout << "arr[index]: " << int_to_binary_string(arr[index]) << " - binary_num: " << int_to_binary_string(binary_num);
arr[index] = arr[index] | binary_num;
cout << " - after: " << int_to_binary_string(arr[index]) << endl;
}
else {
int binary_num = 0 | 255;
int binary_num_2 = pow(2,bit_number);
binary_num = binary_num^binary_num_2;
arr[index] = arr[index] & binary_num;
}
}
// --- Constructor ---
BoolArray::BoolArray(int size) :
size(size)
{
size = ceil((double)size/8);
arr = new unsigned char[size];
}
// --- Destructor ---
BoolArray::~BoolArray() {
delete[] arr;
}
// --- operator[] ---
MyBoolean BoolArray::operator[](unsigned int index) {
if(index>size-1)
throw "error";
int arr_index = floor((double)index/8);
int bit_number = index - arr_index*8;
unsigned int binary_num = pow(2,bit_number);
int value= false;
if((arr[arr_index] & binary_num)>0)
value = true;
MyBoolean my_bool(value,index,this);
return my_bool;
}
// --- size ---
int BoolArray::getSize() const {
return size;
}
ostream& operator<<(ostream& os, MyBoolean b) {
os << (bool) b;
return os;
}
ostream& operator<<(ostream& os,BoolArray& arr) {
for(int i=0;i<arr.getSize();i++)
os << arr[i] << "->";
return os;
}
int main() {
BoolArray arr(12);
arr[0] = true;
arr[1] = false;
arr[2] = true;
arr[3] = false;
arr[4] = true;
arr[5] = false;
arr[6] = true;
arr[7] = false;
arr[8] = true;
arr[9] = false;
arr[10] = true;
arr[11] = false;
cout << arr;
return 0;
}