C + + Multiple inheritance

Source: Internet
Author: User

Multiple inheritance

the C + + primer 3th " p794~798

To support multiple inheritance, a derived table of a class:

Class Bear:public Zooanimal {...};

is expanded into a base class table that supports comma partitioning. For example:

Class Panda:public Bear, public endangered {...};

Each listed base class must also specify its access level: public, protected, or one of the private. As with single inheritance, only when the definition of a class has already appeared can it be listed in the multiple-inheritance base-class table.

C + + has no restrictions on the number of base classes for a derived class. In fact, two base classes are the most common, and a base class is often used to represent a public abstract interface, and another base class provides a private implementation. Derived classes inherited from three or more direct base classes follow mixin-based

Design style, where each base class represents an aspect of the full interface of the derived class.

Order of Construction

Under multiple inheritance, a derived class contains a base class child object for each base class. For example, when we write:

Panda Ying_yang;

, Ying_yang consists of a Bear class child object (which also contains a Zooanimal base class sub-object), a endangered class child object, and non-static data members declared in the Panda class:

Multiple Inheritance Panda Hierarchical Structure

The order in which the base class constructors are called is governed by the order in which they are declared in the class-derived table. For example, for Ying_yang, the constructor is called the Bear constructor (because the bear is derived from zooanimal, so before the Bear constructor executes, the Zooanimal constructor is called first), the endangered constructor, Then the panda constructor.

The order of the constructor calls is not affected by the presence of the base class in the member initialization table and the order in which it is listed, that is, if the bear default constructor is called implicitly, it does not appear in the Member initialization table, as follows:

The bear default constructor is called before the dual-argument constructor of the endangered function

Panda::P anda (): Endangered (endangered::environment,endangered::critical)

{ ... }

Then the default constructor for bear is still called before the two-parameter endangered constructor is explicitly listed. Similarly, the sequence of destructors calls is always the opposite of the constructor order. In our example, the sequence of destructor calls is: ~panda (), ~endangered (), ~bear (), and finally ~zooanimal ().

Ambiguity of member functions with the same name

Under single inheritance, the 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 bear and endangered define a member function print (), the following statement:

Ying_yang.print (cout);

will cause compile-time errors, even if the two inherited member functions define different parameter types:

Error:ying_yang.print (cout)--ambiguous, one of

Bear::p rint (ostream&)

Ndangered::p rint (ostream&, int)

The reason is that inherited member functions do not form overloaded functions in derived classes, so for print () calls, the compiler uses only the name resolution for print when parsing, instead of using the overloaded solution based on the actual argument type passed to print ().

Transformations with multiple base classes

Under single inheritance, a pointer or reference to a derived class can be automatically converted to a pointer or reference to a base class, if necessary. For multiple inheritance, this is also true. For example, a panda pointer or reference can be converted to a pointer or reference to the Zooanimal, bear, or endangered class. For example:

extern void display (const bear&);

extern void Highlight (const endangered&);

Panda Ying_yang;

Display (Ying_yang); Ok

Highlight (Ying_yang); Ok

extern ostream& operator<< (ostream&, const zooanimal&);

cout << Ying_yang << Endl; Ok

However, the probability of two conversions under multiple inheritance is very high. For example, consider the following two functions:

extern void display (const bear&);

extern void display (const endangered&);

Call display () with an unqualified decorated Panda object:

Panda Ying_yang;

Display (Ying_yang); Error: Ambiguity

will result in the following general form of compile-time errors:

Error:display (Ying_yang)--ambiguous, one of

extern void display (const bear&);

extern void display (const endangered&);

The compiler has no way of distinguishing which direct base class should be used (the compiler will not attempt to differentiate between base class conversions based on a derived class conversion, as well as converting to each base class).

Virtual functions under Multiple inheritance

To understand how multiple inheritance affects virtual function mechanisms, let's define a set of virtual functions for each Panda direct base class:

Class Bear: public Zooanimal {

Public

Virtual ~bear ();

Virtual ostream& Print (ostream&) const;

Virtual string IsA () const;

// ...

};

Class endangered {

Public

Virtual ~endangered ();

Virtual ostream& Print (ostream&) const;

virtual void highlight () const;

// ...

};

Now let's define panda, which provides its own print () instance, destructor, and introduces a new virtual function cuddle ():

Class Panda : Public bear, public endangered

{

Public

Virtual ~panda ();

Virtual ostream& Print (ostream&) const;

virtual void cuddle ();

// ...

};

The set of virtual functions that can be called directly from the Panda object is shown in the following table:

Name of virtual function

Activity instances

Destructor( structure function )

Panda::~panda ()

Print (ostream&) const

Panda::p rint (ostream&)

IsA () const

Bear::isa ()

Highlight () const

Endangered::highlight ()

Cuddle ()

Panda::cuddle ()

When a bear or zooanimal pointer or reference is initialized or assigned with the address of the Panda class object, the "Panda-specific" and "endangered" portions of the Panda interface are no longer accessible. For example:

Bear *PB = new Panda;

Pb->print (cout); Ok:panda::p rint (ostream&)

Pb->isa (); Ok:bear::isa ()

Pb->cuddle (); Error: Not part of the Bear interface

Pb->highlight (); Error: Not part of the Bear interface

Delete PB; Ok:panda::~panda ()

Similarly, when a pointer or reference is initialized or assigned with the address of an Panda class object, the "Panda-specific" and "bear" portions of the Panda interface are no longer accessible. For example:

Endangered *pe = new Panda;

Pe->print (cout); Ok:panda::p rint (ostream&)

Pe->isa (); Error: Not an interface part of endangered

Pe->cuddle (); Error: Not an interface part of endangered

Pe->highlight (); Ok:endangered::highlight ()

Delete pe; Ok:panda::~panda ()

No matter what type of pointer we use to delete an object, the processing of the virtual destructor is consistent. For example:

Zooanimal *pz = new Panda;

Delete PZ;

Bear *PB = new Panda;

Delete PB;

Panda *pp = new Panda;

Delete pp;

Endangered *pe = new Panda;

Delete pe;

In the above example, the destructor is called in exactly the same order. Destructors are called in the opposite order of constructors: Panda destructors are called through virtual mechanisms. The endangered, bear, and Zooanimal destructors are called statically after the panda destructor is executed.

C + + 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.