Don't walk by the river-also talk about C ++ multi-Inheritance

Source: Internet
Author: User

This is the first time I used the multi-INHERITANCE OF C ++ and suffered a major setback. I was born stupid and did not have a strong understanding, so I never dared to touch this thing before. This time, I was forced to drive evil ideas. Not much nonsense. Please refer to the following situations:

I have a class

Class {

STD: vector <int> VEC;

};

Another Class B

Class B {

;

};

Now we use Class C to inherit a and B,

Class C: Public,

Public B {

;

};

Now I have an instance:

A * A = new C;

For some reason, I want to call the function B. I only have the following:

(B *) A-> Foo ();

In this case, if foo is useful to class member variables, the variable address may be incorrectly Accessed at this time. (Or errors may not occur. Everything depends on the compiler)

The perfect method is as follows:

(C *) A-> Foo ();

In this way, there will be no problems. But as you can see, this method is too ugly. It seems that the existence of multiple inheritance itself is redundant. We always have a way to avoid it, just like using Java. But the magic of C ++ is here to provide you with infinite possibilities and let you try, but the consequence may be heaven or the abyss.

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.