Download presentation
Presentation is loading. Please wait.
1
Virtual base, constructor & destructor
Kei Hasegawa
2
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
3
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
4
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 } };
5
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 };
6
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
7
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 } };
8
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. };
9
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.
10
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
11
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.