Reputation: 6969
I am dusting off my C++ learnings and trying to write a program here.
class Quad{
public:
Quad(){}
protected:
vec _topLeft, _topRight, _bottomLeft, _bottomRight;
};
class IrregularQuad : public Quad{
public:
IrregularQuad(vec topLeft, vec topRight, vec bottomLeft, vec bottomRight)
: _topLeft(topLeft), _topRight(topRight), _bottomLeft(bottomLeft), _bottomRight(bottomRight)
{}
};
I am getting a compile error on the above Dervied class contractor saying: Member initializer _topLeft does not name a non-static data memeber or base class (similar error for other members as well)
I can't get my head around what's going worng. Is it that I can't initialise protected members using Initalizer list or something?
Upvotes: 2
Views: 2124
Reputation: 19282
This is a duplicate of error C2614: 'ChildClass' : illegal member initialization: 'var1' is not a base or member
You can only initialise members or base classes in the initialiser list.
Upvotes: 0
Reputation: 55425
Is it that I can't initialise protected members using Initalizer list or something?
Right. Only class' own members can be initialized in constructor initializer list (You can, OTOH, assign to them in constructor's body). The base subobjects are initialized first.
You'll need to somehow delegate the work to one of base class' constructors:
class Base {
explicit Base(int i) : m(i)
{}
protected:
int m;
};
class Derived : public Base {
explicit Derived(int i) : Base(i)
{ }
};
Upvotes: 3
Reputation: 35208
You don't initialize base class members in a derived class' initializer list. You can add a constructor to Quad
to do that for you, or you can set them yourself in the body of the derived class constructor.
Upvotes: 1