Virtual base, constructor & destructor 2019.07.12 Kei Hasegawa
Example V A B C struct C : A, B { }; struct V { ... C(int c) : A(c) {} V(int); ~V(); }; struct A : virtual V { A(int a) : V(a) { ... } struct B : virtual V { ~B(); V A B C
Constructor of C Call default constructor of V that is common part Nothing to be done if there doesn’t exist constructor of V It’s error if V has non-default construct and doesn’t have default constructor Call special version constructor of A and B which are base class of C: not call constructor of V
Constructor of A struct A : virtual V { }; ... A(int a) : V(a) { ... } Special version constructor Called from constructor of C Generated by compiler Same with A(int) in spite of not calling constructor of V } };
Consturctor of B Basically same with A struct B : virtual V { }; ... Generated by compiler B has virtual base V or V constructor is declared } B’() { Special version Called from construct of C Same with B() in spite of not calling constructor of V };
Destructor of C Call special version destructor of B and A Not calling destructor of V Call destructor of V which is common part Nothing to be done if V has no destructor
Destructor of B struct B : virtual V { }; ... ~B(); ~B’() { } Generated by compiler Same with ~B() in spite of not calling ~V() Called from constructor of C } };
Destructor of A Basically same with B struct A : virtual V { }; ... Generated by compiler ~V() is declared } ~A’() { Called from destructor of C Same with ~A() in spite of not calling ~V() If there exists nothing to be done, calling from destructor of C can be omitted. };
Implementation note Number of special versions constructor & destructor is 2^n – 1, where n is number of virtual base struct A : virtual V1, virtual V2, ..., virtual Vn { ... }; All these special versions shouldn’t be generated at compiling of A When struct C is declared like example, special versions constructor & destructor of A and B are required. So, it’s reasonable to generate them when they are require.
Implementation abstract (1) Enable to specify exclude list when generating base class constructor & destructor struct V at example Suppress inline substitution for exchanging special version constructor & destructor C::C(int c) : A(c) { ... } A(c) will be exchanged to special version
Implementation abstract (2) Constructor & destructor of class which has virtual base Generate inline function for body part struct A : virtual V { ... A(int a) : V(a) { /* Code here saved */ } A.body(int a) { /* at this inline function */ } }; For special version of A::A(int)