I have this class:
class A : public B
and i need to add some protected field: _field, in A and I can’t touch/change B.
Now, all the function in B and A are virtual except the constructor. Obviously, _field is not part of class B.
I need to initialize _field. How can i do that if the only constructor is B’s?
Also, something like this:
unsigned long _field = 0;
gives me an error compilation.
I solve this by:
class A : public B
{
protected:
unsigned long _field;
public:
void fooFunction(){
....do other stuff....
static bool isInitField = false;
if (!isInitField){
_field = 0;
isInitField = true;
}
...rest of the function...
}
Is there a better way to do that without using static?
Thanks,
Or
Use A constructor and call B constructor in the initialization list, this way you initialize all fields of class A: