Virtual base class and its derived constructor and destructor u

Source: Internet
Author: User

The general sequence of constructing a class object is:

(1): if there is a direct or indirect virtual base class, execute the constructor of the virtual base class first.

(2): If it is a base class, they execute their constructor in the order they appear in the inherited declaration list, but during the constructor, the constructor of the virtual base class is no longer executed.

[Csharp]
# Include <iostream>
Using namespace std;
Class base0 {
Public: www.2cto.com
Base0 (int var): var0 (var) {cout <"ggggg" <endl ;};
Int var0;
Void fun0 () {cout <"memory of base0" <endl ;}
~ Base0 () {cout <"gggg" <endl ;}
};
Class base1: virtual public base0 {
Public:
Base1 (int var): base0 (var) {cout <"hhhhh" <endl ;}
Int var1;
~ Base1 () {cout <"HHHH" <endl ;}
};
Class base2: virtual public base0 {
Public:
Base2 (int var): base0 (var) {cout <"tttttt" <endl ;}
Int var2;
};
Class mm: public base1, public base2 {
Public:
Mm (int var): base1 (var), base0 (var), base2 (var) {cout <"ppppp" <endl ;}
Int var;
Void fun () {cout <"member of mm" <endl ;}
~ Mm () {cout <"ppppp" <endl ;}
};
Int main (){
Mm a (1 );
A. var = 2;
A. fun ();
Return 0;
}

 

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.