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\n");
}
SomeDerivedClass::SomeDerivedClass(int value) : SomeParentClass(value), SomeOtherParentClass(value) {
printf("SomeDerivedClass(%d)\n", 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()\n"); }
~Base() {}
protected:
Base(int value) { printf("Base(%d)\n", value); }
};
class Derived1 : virtual public Base {
public:
Derived1() : Base(0) { printf("Derived1()\n"); }
~Derived1() {}
protected:
Derived1(int value) : Base(value) { printf("Derived1(%d)\n", value); }
};
class Derived2 : virtual public Derived1 {
public:
Derived2() : Derived1(0) { printf("Derived2()\n"); }
~Derived2() {}
protected:
Derived2(int value) : Derived1(value) { printf("Derived2(%d)\n", value); }
};
int main() {
Derived2* NewDerived2 = new Derived2();
}
Result:
Base()
Derived1(0)
Derived2()