Problem:
class Base {
public:
Base(Base* pParent);
/* implements basic stuff */
};
class A : virtual public Base {
public:
A(A* pParent) : Base(pParent) {}
/* ... */
};
class B : virtual public Base {
public:
B(B* pParent) : Base(pParent) {}
/* ... */
};
class C : public A, public B {
public:
C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here
/* ... */
};
At the position given, gcc complains that it cannot match function call to Base(), i.e. the default constructor. But C doesn't inherit directly from Base, only through A and B. So why does gcc complain here?
Ideas? TIA /Rob
virtual
base classes are special in that they are initialized by the most derived class and not by any intermediate base classes that inherits from the virtual base. Which of the potential multiple initializers would the correct choice for initializing the one base?
If the most derived class being constructed does not list it in its member initalization list then the virtual base class is initialized with its default constructor which must exist and be accessible.
Note that a virtual base identifier is allowed to be use in a constructor's initializer list even if it is not a direct base of the class in question.
You need to explicitly call the constructor for Base from C:
class C : public A, public B {
public:
C(C* pParent) : Base(pParent), A(pParent), B(pParent) {}
/*... */
};
If you declare a custom constructor, the default constructor is disabled. In virtual inheritance you need to call the virtually inherited constructor directly because otherwise it would not know whether to initialize by A or by B.
Problem:
class Base {
public:
Base(Base* pParent);
/* implements basic stuff */
};
class A : virtual public Base {
public:
A(A* pParent) : Base(pParent) {}
/* ... */
};
class B : virtual public Base {
public:
B(B* pParent) : Base(pParent) {}
/* ... */
};
class C : public A, public B {
public:
C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here
/* ... */
};
At the position given, gcc complains that it cannot match function call to Base(), i.e. the default constructor. But C doesn't inherit directly from Base, only through A and B. So why does gcc complain here?
Ideas? TIA /Rob
virtual
base classes are special in that they are initialized by the most derived class and not by any intermediate base classes that inherits from the virtual base. Which of the potential multiple initializers would the correct choice for initializing the one base?
If the most derived class being constructed does not list it in its member initalization list then the virtual base class is initialized with its default constructor which must exist and be accessible.
Note that a virtual base identifier is allowed to be use in a constructor's initializer list even if it is not a direct base of the class in question.
You need to explicitly call the constructor for Base from C:
class C : public A, public B {
public:
C(C* pParent) : Base(pParent), A(pParent), B(pParent) {}
/*... */
};
If you declare a custom constructor, the default constructor is disabled. In virtual inheritance you need to call the virtually inherited constructor directly because otherwise it would not know whether to initialize by A or by B.
0 commentaires:
Enregistrer un commentaire