I'm having a problem with the constructors of a very complex class structure that is spread over several files. Each class has a public default contructor without arguments and a protected contructor with arguments. Each constructor of any derived class is calling the protected constructor of it's parent with arguments. The following example is the same for all derived classes in the structure (the number of parent classes may differ).
file example.h:
class SomeDerivedClass : virtual public SomeParentClass, virtual public SomeOtherParentClass {
public:
SomeDerivedClass();
protected:
SomeDerivedClass(int value);
}
file example.cpp:
SomeDerivedClass::SomeDerivedClass() : SomeParentClass(0), SomeOtherParentClass(0) {
printf("SomeDerivedClass
");
}
SomeDerivedClass::SomeDerivedClass(int value) : SomeParentClass(value), SomeOtherParentClass(value) {
printf("SomeDerivedClass(%d)
", value);
}
When I construct a class, all the direct virtual parents are constructed with the protected constructor that has arguments. But even though that protected construcor of the parents should also call the protected constructor of it's own virtual parents, the grandparents are always constructed with the default constructor.
Minimal example:
#include <stdio.h>
class Base {
public:
Base() { printf("Base()
"); }
~Base() {}
protected:
Base(int value) { printf("Base(%d)
", value); }
};
class Derived1 : virtual public Base {
public:
Derived1() : Base(0) { printf("Derived1()
"); }
~Derived1() {}
protected:
Derived1(int value) : Base(value) { printf("Derived1(%d)
", value); }
};
class Derived2 : virtual public Derived1 {
public:
Derived2() : Derived1(0) { printf("Derived2()
"); }
~Derived2() {}
protected:
Derived2(int value) : Derived1(value) { printf("Derived2(%d)
", value); }
};
int main() {
Derived2* NewDerived2 = new Derived2();
}
Result:
Base()
Derived1(0)
Derived2()
question from:
https://stackoverflow.com/questions/65644480/wrong-constructor-called-for-virtual-base-class-of-virtual-base-class 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…