Consider a class with a number of bool attributes
class A
{
bool a;
bool b;
bool c;
bool d;
bool e;
bool f;
};
While each bool
object could be represented with a single bit, here each attribute would take a byte (if I am not mistaken). The object would take 6 bytes instead of just 1 byte (6 bits of which would be actually used). The reason being that bits are not addressable, only bytes are.
To condensate the memory a bit, one could use a vector<bool>
or a bitset
and then access the attributes, by their indices. For example, one could write a get function as
bool A::get_d() {data[3];}
Ideally, I would love being able to directly access the attributes with InstanceOfA.d
. Is it possible to do that, while ensuring that all of my 6 bool
are being condensed within the same byte?