In C structs, it is possible to specify another bit length than the default bit length of the type like this:
struct MyStruct{ int myVar : 1; //Size of myVar is 1 bit (so it can take values 0 or 1 int myOtherVar: 4; //Size of myOtherVar is 4 bits (so it can take values 0 to 15) } This is called bit fields.
My question is if it is also possible to do this in C++ classes, like this:
class MyClass{ public: //Some methods private: int m_myAttribute : 1; int m_myOtherAttribute : 4; } I searched the web for this but all the examples I found of bit fields used structs, not classes.
I tested this code and it compiled just fine, but I would like to know if the size of the attributes are really the specified size or if the compiler just ignored the bit fields and used the standard int size.
sizeof()function. Also, as far as I know, the only difference between structs and classes are, default class variable will be private, default struct variable will be public. You should read here: [stackoverflow.com/a/7762179/1867076]sizeof().sizeof()doesn't work with bit fields (see the link that Roy T. posted)