C ++ virtual base class solves the problem of ambiguity and execution Result Analysis

Source: Internet
Author: User

Because c ++ allows many inheritance, the problem of ambiguity arises: Multiple derived Classes define methods with the same name, then, when the main function calls this method through the base class, the compiler will be hard. Because it does not know which method in the derived class you want to use.

Now there is another question: if there are two Derived classes deriveda and derivedb, one of their methods with the same name has a parameter, and the other has no parameter, isn't there a problem of ambiguity?

In other words, either the private or public methods of the two Derived classes can prevent ambiguity?

The answer is no. The ambiguity check should be performed before the access control permission or type check. Therefore, different access control permissions or types cannot solve the ambiguity problem..

Since the above assumptions are true, how can we effectively solve the problem of ambiguity?

The virtual base class is proposed to solve this problem.

The main cause of ambiguity is that the base class produces two sub-objects in the derived class, which leads to non-uniqueness of access to the base class members. Therefore, you only need to make the public base class generate only one sub-object.

That is to say, make sure that the virtual base class constructor is called only once. The self-object of the virtual base class is implemented by the constructor of the most derived class by calling the constructor of the virtual base class,The call to the constructor of the virtual base class listed in all base classes of the most derived class will be ignored during execution, and the self-object Value of the virtual base class will be initialized once. At the same time, the initialization list of the most derived class must list calls to the virtual base class constructor. If not, it indicates that the default constructor of the virtual base class is used.

(In the hierarchy of the inheritance structure, the created object belongs to a class in the middle of the structure, and the class specified when the object is created is the most derived class)

The following is a small example:

# Include <iostream> using namespace STD; // base class base {// public member public: Base (char I) {cout <"base's cons. "<I <Endl ;}~ Base () {cout <"base's Des. "<Endl ;}}; // derived class 1 class derivedl1: virtual public base {// public member public: derivedl1 (char I, char J): Base (I) {cout <"derivedl1's cons. "<j <Endl ;}~ Derivedl1 () {cout <"derived's Des. "<Endl ;}// private member private: Char B ;}; // derived class 2 class derivedl2: virtual public base {// public member public: derivedl2 (char I, char J): Base (I) {cout <"derivedl2 'cons. "<j <Endl ;}~ Derivedl2 () {cout <"derived12 'des. "<Endl ;}}; // inherits the derived class derived2: Public derivedl1, public derivedl2 of the base class {// public member public: derived2 (char I, char J, char K, char L, char M, char N): derivedl2 (K, L), derivedl1 (I, j), base (I), AA (m) {cout <"derived2's cons. "<n <Endl ;}~ Derived2 () {cout <"derived2's Des. "<Endl ;}// private member private: Base AA ;}; // main function int main () {derived2 OBJ ('A', 'B ', 'C', 'D', 'E', 'F'); Return 0 ;}

Define the derived2 Class Object OBJ in main () and call its constructor. Because this class has a member object AA, and according to C ++, the derived class first calls the virtual base class base, and then calls two directly base classes derivedl1 and derivedl2; then, call the constructor of the member object to initialize the member object AA, and then execute the constructor of the derived class derived2.

The execution sequence of the base class depends on the sequence in which the base class is declared when the derived class is defined, but it is irrelevant to the initialization list sequence of the derived class constructor. Therefore, run derivedl1 first, and then run derivedl2.

The derivedl1 class is also a derived class that inherits the base virtual base class. Because its example already exists in derived2, subsequent calls will not be executed.

At the same time, the execution sequence of the Destructor is strictly opposite to that of the constructor. Therefore, the execution solution of this program is:

This article mainly introduces a solution to solve the problem of ambiguity in C ++-virtual base class. Hope to leave valuable comments on passing children's shoes.

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.