Eliminate inheritance ambiguity using virtual inheritance

Source: Internet
Author: User

Problem description:

 

1. Assume that there is a base class cbase

2. Class cinherit1 inherits this base class

3. Class cinherit2 inherits this base class

4. cfinalinherit inherits both cinherit1 and cinherit2.

// 5. Now we need to use the cbase * P pointer to point to the new cfinalinherit object

 

CodeAs follows:

# Include <iostream> <br/> using namespace STD; <br/> // base class <br/> class cbase <br/>{< br/> public: <br/> cbase () {cout <"cbase constructor. "<Endl ;}< br/> virtual ~ Cbase () {cout <"cbase deconstructor. "<Endl ;}< br/>}; <br/> // The first inheritance class <br/> class cinherit1 <br/>: public cbase <br/>{< br/> Public: <br/> cinherit1 () {cout <"cinherit1 constructor. "<Endl ;}< br/> virtual ~ Cinherit1 () {cout <"cinherit1 deconstructor. "<Endl ;}< br/>}; <br/> // The second inheritance class <br/> class cinherit2 <br/>: public cbase <br/>{< br/> Public: <br/> cinherit2 () {cout <"cinherit2 constructor. "<Endl ;}< br/> virtual ~ Cinherit2 () {cout <"cinherit2 deconstructor. "<Endl ;}< br/>}; <br/> // final inheritance class <br/> class cfinalinherit <br/>: Public cinherit1 <br/>, public cinherit2 <br/>{< br/> Public: <br/> cfinalinherit () {cout <"cfinalinherit constructor. "<Endl ;}< br/> ~ Cfinalinherit () {cout <"cfinalinherit deconstructor. "<Endl ;}< br/>}; <br/> // entry function <br/> void main () <br/>{< br/> cout <"Call constructor:" <Endl; <br/> cfinalinherit * PFI = new cfinalinherit (); <br/> cout <Endl <"Call destructor:" <Endl; <br/> Delete PFI; <br/>}< br/>

 

The above code implements 1 ~ The output is as follows:

 

Call the constructor: <br/> cbase constructor. <br/> cinherit1 constructor. <br/> cbase constructor. <br/> cinherit2 constructor. <br/> cfinalinherit constructor. <br/> call the Destructor: <br/> cfinalinherit deconstructor. <br/> cinherit2 deconstructor. <br/> cbase deconstructor. <br/> cinherit1 deconstructor. <br/> cbase deconstructor.

 

If you cancel the comment in step 5. That is:

 

Cbase * PFI = new cfinalinherit ();

 

An error occurs, as shown below (MS vc9.0 ):

 

Error c2594: "initialization": the conversion from "cfinalinherit *" to "cbase *" is not clear

 

In this case, we need to use the virtual Inheritance Mechanism of C ++ to eliminate this ambiguity.

Modify cinherit [12] to inherit from cbase as follows: virtual public cbase

 

The execution result is as follows:

 

Call the constructor: <br/> cbase constructor. <br/> cinherit1 constructor. <br/> cinherit2 constructor. <br/> cfinalinherit constructor. <br/> call the Destructor: <br/> cfinalinherit deconstructor. <br/> cinherit2 deconstructor. <br/> cinherit1 deconstructor. <br/> cbase deconstructor.

 

In this way, we can see that virtual inheritance is "wise" by looking at the output results ". It can identify whether the cbase has been constructed. If yes, it does not need to be constructed for the second time. Normal inheritance does not have this function, so after being constructed for cinherit1, cinherit2 will be constructed again after being "closed with eyes.

 

 

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.