Reputation: 23172
As I understand it, when I initialise the base class in the derived class initialiser list, the base class is immediately constructed and base class elements should then be available. If this is right, why doesn't this work?
class Base
{
public:
int elem;
}
class Derived : public Base
{
Derived() : Base(), elem(1) {}
// error: class 'Derived' does not have any field named 'elem'
}
NOTE: In my case I cannot make any changes to Base
(it is a fixed interface class).
Upvotes: 2
Views: 175
Reputation: 9944
In that case, you can not initialize elem in the initializer list, and must instead initialize it in the function body.
class Base {
public:
int elem;
};
class Derived : public Base {
public:
Derived() : Base() {
elem = 1;
}
};
Upvotes: 0
Reputation: 254771
The initialiser list is used to initialise the direct members and base sub-objects of that class, not members of base classes. Base class members are initialised by the base class constructor, and you can't initialise anything twice.
If you want to reassign the base-class member after it's been initialised, then you can:
Derived() : Base() {elem = 1;}
or you could allow the value to be passed through to the base class's constructor:
explicit Base(int e) : elem(e) {}
Derived() : Base(1) {}
although the note you've added to the question indicates that that's not an option in your particular circumstances.
Upvotes: 2
Reputation: 114845
You can only initialize members of the class and base classes in initialization list NOT base class members.
If you want to override the value of a base class member do it in the constructor's body.
Upvotes: 0
Reputation: 1159
elem belongs to class Base, so Base is responsible for initializing it.
The correct code looks like this:
class Base
{
public:
int elem;
Base(int n) : elem(n) {}
};
class Derived : public Base
{
Derived() : Base(1) {}
};
Upvotes: 0
Reputation: 477710
A constructor can only construct its own class's members. You need to give Base
a suitable constructor:
class Base
{
public:
Base() : elem(23) { }
int elem;
protected:
Base(int n) : elem(n) { }
};
class Derived : public Base
{
Derived() : Base(1) {}
};
Upvotes: 2