I have a class and need to make the size of one of its protected members publicly available.
I cannot make the field public and don't want to introduce a new field, so I am trying to introduce an enum and assign it to the size of the field, like so:
class MyObject
{
public:
enum
{
myFieldSize = sizeof(myField),
};
protected:
uint8_t myField;
}
However my compiler tells me that it can't find a declaration for "myField". Is this the expected behavior? I have other public functions that access myField, why is the enum seemingly unable to do so?
This is one of those instances where the order of what you have in your class matters: myField
does not exist at the point sizeof(myField)
is reached.
A workaround is to have uint8_t myField;
above the enum
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With