Initialize the virtual base class

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.