I am a little confused about class and superclass sharing fields. I was expecting this to be ok:
class SuperC {
public:
SuperC();
protected:
double value;
};
class C : public SuperC {
public :
C(double value);
};
SuperC::SuperC(){}
C::C(double value):SuperC(),value(value){}
but the compiler tells me C has no field "value". C does not inherit from the one defined in SuperC ?
many thx
It does, but you can only initialize current class members using the constructor initialization list syntax.
You'll have to create an overloaded constructor in SuperC
that initializes value
and call that.
class SuperC {
public:
SuperC();
SuperC(double v) : value(v) {}
protected:
double value;
};
class C : public SuperC {
public :
C(double value);
};
SuperC::SuperC(){}
C::C(double value):SuperC(value){}
You can't initialize base class members in constructor initialization list of derived class.
fix1: At maximum You can initialize base class(BC) constructor in derived class by passing paramerter to BC.
fix2: Assign base class members in body of derived class constructor instead of constructor initialization list
C::C(double value1):SuperC()
{
value = value1;
}
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