C ++ multi-Inheritance

Source: Internet
Author: User

1. Concept of Multi-inheritance: A class has two or more base classes.

Form: class <类名> : <访问属性> Class <类名> , <访问属性> Class <类名>

For example, class A: public class B, public class C

In the above definition, if both B and C have the print function, then the Class A object will call the print function, which is A defect in Multi-inheritance, to avoid this ambiguity, you can use the domain access character:, for example, Class A's object a accesses the print function in Class B. You can use. b: print ().

2. Under Multi-inheritance, a base class can appear multiple times in the hierarchy, for example, the following inheritance Structure


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + QcDgxcnJ + signature + RMDgtcS21M/Signature/bP1rb + signature/Signature + signature/nA4KOs1eLR + signature/Signature + signature/J0tTN6sirz/uz/ bb + 0uXQ1MLwo7 + release/uz/release/dbWx + m/9da-vcd4kpha + release/release + release/T2rmyz + release/bP1rb + release + IDwvcD4KPHA + z8LD5srHy7XD97XEtPrC66O6PC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> # include Using namespace std; class Base {public: Base (int t): x (t) {} void print () {cout <

3. Construct sequence and destructor sequence in virtual inheritance

In the inheritance hierarchy of the above four classes of ABCD, the calling sequence of the constructor is A-B-C-D, and the calling sequence of the Destructor is just the opposite.

In a more complex inheritance structure, the virtual base class and common base class appear at the same time. The call priority of the virtual base class is higher than that of the non-virtual base class, no matter what inheritance level the virtual base class is located in, the constructor of the virtual base class is called before the non-virtual base class.


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.