C ++ multi-inheritance: the sequence in which constructors are called in the diamond inheritance

Source: Internet
Author: User

C ++ multi-inheritance: the sequence in which constructors are called in the diamond inheritance
In C ++, multiple inheritance inevitably leads to diamond inheritance, that is, the two base classes of the inheritance class are the same base class at the same time. When an object is created, in what order do they call Constructors. If no virtual inheritance is performed: class Base {public: Base () {cout <"Base default constructor call" <endl;} Base (int I) {cout <"Base parameter constructor call" <endl; cout <I <endl;} virtual ~ Base () {}}; class Base1: public Base {public: Base1 (int I, int j = 0): Base (j) {cout <"Base1 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base1 () {}}; class Base2: public Base {public: Base2 (int I): Base (I) {cout <"Base2 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base2 () {}}; class Drived: public Base1, public Base2 {public: Drived (int a, int B, int c, int d): Base1 (), base2 (B) {} virtual ~ Drived () {}}; virtual inheritance (in virtual inheritance, the call to the Base constructor in Base1 Base2 does not work, and the call to the Base constructor is directly undertaken by the derived class, if Drived is not explicitly specified, the default non-parameter constructor is called.): <br> class Base {public: Base () {cout <"Base default constructor call" <endl;} Base (int I) {cout <"Base parameter constructor call" <endl; cout <I <endl;} virtual ~ Base () {}}; class Base1: virtual public Base {public: Base1 (int I, int j = 0): Base (j) {cout <"Base1 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base1 () {}}; class Base2: virtual public Base {public: Base2 (int I): Base (I) {cout <"Base2 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base2 () {}}; class Drived: public Base1, public Base2 {public: Drived (int a, int B, int c, int d): Base1 (), base2 (B) {} virtual ~ Drived () {}}; if the Drived class has a member function of the Base1 Base2 type: class Drived: public Base1, public Base2 {public: Base1 mem1; Base2 mem2; Drived (int, int B, int c, int d): Base1 (a), Base2 (B), mem1 (c), mem2 (d) {} virtual ~ Drived () {}}; if you specify the Base constructor In the Derived class: class Drived: public Base1, public Base2 {public: Base1 mem1; Base2 mem2; Drived (int, int B, int c, int d): Base1 (a), Base2 (B), Base (a), mem1 (c), mem2 (d) {} virtual ~ Drived (){}};

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.