C ++ multi-inheritance and virtual base classes and comparison with. net

Source: Internet
Author: User

Multi-Inheritance
The derived classes we introduced earlier have only one base class, which is called single-base Derivation or single inheritance. In practical use, we often need a derived class to have multiple base classes at the same time. This method is called multi-base Derivation or multi-inheritance.
2.1 Statement of multiple inheritance:
In C ++, the form of a derived class with more than two base classes is similar to that of a single base class. You only need to separate multiple base classes to be inherited with commas.
In multi-inheritance, public and private derivation have the same accessibility for the base class members in the derived class as the rule for single inheritance.
In addition, access to base class members must be unambiguous. If two base classes have data members or member functions with the same name, use the member name limitation to eliminate ambiguity, if the new member or member function in the derived class has the same name as the base class member or member function, the derived class will overwrite the member with the same name as the outer class, And the scope identifier must also be used.
2.2 multi-inheritance constructor and destructor:
The definitions of Multi-inheritance constructor are similar to those of single-inheritance constructor. Only constructors of N base classes are separated by commas.
The execution sequence of the Multi-inheritance constructor is the same as that of the single-inheritance constructor. The constructor of the object member is also executed after the base class constructor is executed, finally, execute the principles of the derived class constructor. Between multiple base classes, the sequence is arranged from left to right according to the declaration of the derived class. The execution sequence of the Destructor is opposite to that of the constructor.
2.3 virtual base class:
If some or all of the direct base classes of a derived class are derived from another common base class, the members inherited from the base class at the upper level will have the same name, when this derived class accesses the members of this common base class, it may produce ambiguity. In this case, you can define a virtual base class. This requires that, in the definition of its direct base class, the common base class should be defined as the virtual base class using the keyword Virtual. The syntax format is as follows:
Class derived class name: base class in Virtual Mode
The initialization of the virtual base class is syntactically the same as that of the general multi-inheritance class, but the calling sequence of the constructor is different. The Calling sequence of the virtual base class constructor is as follows:
1) at the same level, first call the constructor of the virtual base class, and then call the constructor of the non-virtual base class, the constructor of the object member, and the constructor of the derived class.
2) If the same level contains multiple virtual base classes, the constructor of these virtual base classes calls them in the order they are described
3) if the virtual base class is derived from a non-virtual base class, the base class constructor is still called before the derived class constructor is called.

The above is a theory, a bit obscure. The following is an example:

The common interface is a public interface of all classes. The server interface inherits the common interface, the client interface inherits the common interface, and the commonimpl is a public Implementation of the common interface. Serverimpl is the implementation of the server interface, and its common interface implementation directly uses the implementation of the commonimpl class.

If this problem is solved in C #, the compiler will understand that if one class inherits the server interface and the commonimpl implementation, serverimpl only needs to implement specific methods on the server interface. The only restriction is that commonimpl must be placed before the server interface. See the following DEMO code:

Using system; using system. collections. generic; using system. text; namespace demo {interface Ia {string funcina ();} interface IB: IA {string funcinb ();} public class CA: IA {# region Ia member Public String funcina () {return "ca. funcina ";}# endregion} public class CB: Ca, IB {# region IB member Public String funcinb () {return" CB. funcinb ";}# endregion }}

Class CB only needs to implement the method in interface IB. CA and IB must appear in this order;

 

For C ++, the compiler does not know. If you want all subclasses to share a base class or subclass to share a base class of general implementation, then you need to declare the shared part as a virutal base class. In the example, you must inherit the common subclass and declare it as a virtual base class.

#pragma once#include <iostream>using namespace std;class IA{public:    virtual int FuncInA() =0;};class IB:virtual public IA{public:    virtual int FuncInB()=0;};class CA:virtual public IA{public:    virtual int FuncInA()    {        cout<<" CA::FuncInA "<<endl;        return 1;    };};class CB:public CA,public IB{public:    int FuncInB()    {        cout<<"CB::FuncInB"<<endl;        return 2;    };    /*virtual int FuncInA()    {        cout<<"CB::FuncInA"<<"  Call CA::FuncInA() ==>"<<CA::FuncInA()<<endl;        return 3;    };*/};

The above code can be compiled and the result is as expected. If the virtual inheritance modifier is removed, an error is returned.

Related Article

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.