The virtual base class knowledge Point of C + +

Source: Internet
Author: User

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. Class X1:virtual public x{//...}; Class X2:virtual public x{//...}; Initializing virtual base classes (virtual inheritance) is syntactically the same as the initialization of a general multi-inheritance, but the constructor is called in a different order. The constructor of a derived class has a three-principle invocation order: (1) The constructor of the virtual base class is called before the non-virtual base class, (2) If there are multiple virtual base classes in the same hierarchy, the constructors of these virtual base classes are called in the order they are described, and (3) if the virtual base class is derived from a non-virtual base class, the base class constructor is still called first. Call the constructor of the derived class again. When the virtual base class of C + + inherits the base class from the derived class, the virtual keyword is inherited by the dummy key class, such as: Class Derive:virtual public base{}; Virtual base class is mainly resolved in multiple inheritance, the base class may be inherited multiple times, The virtual base class mainly provides a base class for derived classes, such as: Classb{};class d1:public b{};class d2:public b{};class c:public D1, Public d2{}; here C is inherited on D1,d2, but there are two base classes, Cause random. Thus, the virtual base class is used, namely: Classb{};class d1:virtual public b{};class d2:virtual public B{};class c:public D1, Public d2{}; When using virtual base classes, be aware that: (1) A class can be used both as a virtual base class and as a non-virtual base class in a class family. (2) In the object of a derived class, a virtual base class with the same name produces only one virtual base class sub-object, and a non-virtual base class produces its own child object. (3) The virtual base class sub-object is initialized by the constructor of the most derived class by calling the virtual base class's constructor. (4) The most derived class refers to the class that is specified when an object is created in the inheritance structure. (5) The member initialization list of a constructor for a derived class must list the call to the virtual base class constructor, or, if not listed, the default constructor that uses the virtual base class. (6) 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, thus guaranteeing the virtual base class'sThe child object is initialized only once. (7) 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.

Virtual base class knowledge points for 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: 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.