Virtual inheritance C + +

Source: Internet
Author: User

The concept of virtual inheritance in C + +

The common base class is set to the virtual base class in order to solve the data inconsistency problem that the data members with the same name inherited from different paths have different copies in memory. A data member with the same name inherited from a different path has only one copy in memory, and the same function name has only one mapping. This not only solves the two semantic problem, but also saves the memory and avoids the problem of inconsistent data.
Class Derived classes Name: Virtual Inheritance Mode base class name
Virtual is the keyword that declares that the base class is a virtual base class of derived classes.
In the case of multiple inheritance, the virtual base class keyword has the same scope and inheritance as the keyword, and only works on the base class immediately following it.
After a virtual base class is declared, the virtual base class maintains a copy of the same base class sub-object, along with the derived class, throughout the further derivation process.

C + + virtual inheritance

◇ Concept:

C + + uses virtual inheritance (inheritance), which solves the problem of data inconsistency between data members with the same name inherited from different paths, with different copies in memory, and sets the common base class to the virtual base class. A data member with the same name inherited from a different path has only one copy in memory, and the same function name has only one mapping.

◇ Problem Solving:

It solves the problem of two semantics, saves memory and avoids the problem of inconsistent data.
◇ Synonyms:
Virtual base class (to treat a verb as a noun)
When there is a common base class on multiple inheritance paths, the common base class will produce multiple instances (or multiple replicas) at a certain confluence of these paths, and the public base class can be described as a virtual base class if only one instance of the base class is to be saved.

◇ Syntax:

Class Derived classes: Virtual base class 1,virtual base class 2,...,virtual base class N

{

...//derived class member declaration

};

◇ Execution Order

The constructor of the virtual base class is executed first, and the constructors of many virtual base classes are constructed in the order of inheritance;

Executes the constructor of the base class, and the constructors of multiple base classes are constructed in the order in which they are inherited;

Executes the constructor of a member object, and the constructors of multiple member objects are constructed in the order in which they are declared;

Executes the derived class's own constructor;

The destruction is performed in the reverse order of the construction;

Mark

A call to a virtual base class constructor is listed in the member initialization list of a constructor in a derived class that derives directly or indirectly from a virtual base class. However, only the constructor of the most derived class that is used to establish the object calls the constructor of the virtual base class, and the calls to the virtual base class's constructors listed in all base classes of the derived class are ignored in execution, guaranteeing that the virtual base class sub-object is initialized only once.

When a call to a virtual base class and a non-virtual base class constructor occurs at the same time in a member initialization list, the constructor of the virtual base class is executed before the constructor of the non-virtual base class.

◇ Cause and Effect:

Virtual inheritance Resolution--ambiguity multiple inheritance

Virtual inheritance C + +

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.