C + + notes (10): Multiple inheritance

Source: Internet
Author: User

Multiple inheritance

C + + allows a derived class to inherit from another class.

Also allows A derived class inherits multiple base classes at the same time. This is called multiple inheritance.
 
declares methods for multiple inheritance
class d:public A, Public B, public C
{
  /...
};
 
D is a derived class of multiple inheritance. It inherits from a, B, C in the same way as public inheritance.
 
Constructor Construction Order
D:d ()
 :a (), B (), C ()
{
}
  Call Order: the constructor of the base class is called before the constructor of the derived class is executed. The constructor invocation order of the base class follows the order in which the base class appears when the derived class is declared.
destructors are called in reverse order of construction
 
Ambiguity
with member functions of the same name under single inheritance, public and protected members of the base class can be accessed directly, just as they are members of a derived class, which is also true for multiple inheritance. However, under multiple inheritance, a derived class can inherit a member of the same name from two or more base classes. In this case, however, direct access is differentiation and will result in compile-time errors.
For example, if both A and B define a member function print (),
The following statement:
D D;
D.print (); The
causes a compile-time error, even though the two inherited member functions define different parameter types:
because inherited member functions do not form overloaded functions in derived classes, for print () calls, the compiler uses only the print The name resolution. The workaround is to precede the method with the Class D to which it belongs. A::p rint ();
 
Virtual base class
Although the same base class is not allowed to occur two times in a derived list, a derived class can actually inherit the same class multiple times.

Derived classes typically contain sub-sections that correspond to each class on the inheritance chain. In both cases, Class D indirectly inherits Class A two, meaning that class D contains two copies of Class A. Therefore, in a Class D object will contain 2 groups of Class A members, at this time without prefix qualifier directly using a member would throw a "ambiguity" error:
To solve these problems, C + + provides a mechanism for virtual inheritance. The purpose of virtual inheritance is to make a class declare a promise to share its base class. A shared base class sub-object is called a virtual base class. Under this mechanism, no matter how many times the virtual base class appears in the inheritance system, only one shared virtual base class sub-object is included in the derived class. The way we specify the virtual base class is to add the keyword virtual to the derived list:
Class B:virtual Public A
{
}

Class C:virtual Public A
{
}
Class D:public B,public C
{
}
The construction order of objects containing virtual base classes is slightly different from the construction order of the general multiple inheritance: first initialize the virtual base class sub-object (the lowest-layer derived class is responsible), and then initialize the direct base class (non-virtual) sequentially in the order of the derived list.

C + + notes (10): Multiple inheritance

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.