I have a base class with a bitfield
enum Enum {
ENUM1,
ENUM2,
...
ENUM8
};
struct Base {
Enum type : 3;
};
I am inheriting a class
struct Derived : public Base {
bool bf1 : 1;
u32 bf2 : 19,
u32 bf1 : 9;
};
I need to ensure that the memory space for the Derived class is 4 bytes (I have a 32-bit microcontroller), since there will be several inherited classes and they need to be serialized, and when unpacking by the 'type' field, understand how to process the remaining bit fields .
Is this method reliable? If not, how can we do it better?
My CLang compiler does package everything correctly, but I'm not sure about the reliability of this result.
static_assert(sizeof(Derived)==4);
in your code.Enum
is also implementation-defined, although not larger thanint
. You can fix the underlying type by writing e.g.enum Enum : int { /*...*/ };
. Andu32
is not a standard type. You must have defined it somewhere yourself or in a library. The standard type is nameduint32_t
.constexpr <some_type> bit1 = 0x01;
in a namespace