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.