you can overload subscript operator "[]" to return the address of element to be able to assign to.
you can't return a reference to a bit because bits don't have addresses the smallest addressable variable is 1 byte (bool or char) which consists of 8 bits.
#include <iostream>
using namespace std;
class Byte
{
public:
Byte(bool[], int);
~Byte() {delete[] itsBits;}
bool& operator[](int);
private:
bool* itsBits;
int itsLength;
};
Byte::Byte(bool bits[], int length) :
itsLength(length)
{
itsBits = new bool[itsLength];
for(int i(0); i < itsLength; i++)
itsBits[i] = bits[i];
}
bool& Byte::operator [](int index)
{
if(index < 0 || index > itsLength)
return itsBits[0];
else
return itsBits[index];
}
int main()
{
bool bArray[] = {true, false, true, true, false, true, false, true};
Byte theByte(bArray, 8);
cout << theByte[2] << endl;
theByte[2] = false; // invoking bool& operator[] it returns a reference the 3rd element in array so we can assign to it value
cout << theByte[2] << endl;
cout << theByte[6] << endl;
for(int i(0) ; i < 8; i++)
cout << theByte[i] << " ";
cout << endl << endl << endl;
return 0;
}