No matter where the virtual base class appears in the inheritance level, they are constructed before the non-virtual base class.
Initialization of the virtual base class becomes the responsibility of the last-layer inheritance class.
Namespace ex18_15
{
Class base {
Public:
Base () {cout <"base () \ n ";};
Base (string ){
Cout <"base (string) \ n ";
};
Base (const base &){
Cout <"base (const base &) \ n ";
};
//...
Protected:
String _ name;
};
Class derived1:/* virtual */public Base
{
Public:
Derived1 () {cout <"derived1 (): Base () \ n ";}
Derived1 (string S): Base (s) {cout <"derived1 (string): Base (string) \ n ";}
Derived1 (const derived1 & D): Base (d) {cout <"derived1 (derived): Base (base) \ n ";}
};
//
// Class derived2: virtual public Base
//{
// Public:
// Derived2 () {cout <"derived2 (): Base () \ n ";};
// Derived2 (string S): Base (s) {cout <"derived2 (string): Base (string) \ n ";}
// Derived2 (const derived1 & D): Base (d) {cout <"derived2 (derived): Base (base) \ n ";}
//};
Class VMI: Public derived1/*, public derived2 */
{
Public:
VMI () {cout <"VMI () \ n ";}
// If no base (s) exists in the initialization list, the base is constructed using the default constructor.
VMI (string S): derived1 (s)/*, derived2 (s), * // * base (s )*/{
Cout <"VMI (string) \ n ";
}
VMI (const VMI & V):/* derived2 (V), */derived1 (v)/*, base (v )*/{
Cout <"VMI (const VMI &) \ n ";
}
};
Class final: Public VMI
{
Public:
Final () {cout <"final () \ n ";}
// If no base (s) exists in the initialization list, the base is constructed using the default constructor.
Final (string S): VMI (s)/*, base (s )*/{
Cout <"final (string) \ n ";
}
Final (const final & F): VMI (f)/*, base (f )*/{
Cout <"final (const final &) \ n ";
}
};
Inline void test ()
{
Final F;
Cout <Endl;
Final S ("S ");
Cout <Endl;
Final C (f );
}
}