The following article describes in detail the C ++ virtual base class. The so-called C ++ virtual base class is initialized by the constructors of the most Derived classes by calling the constructors of the virtual base class, but the premise is to have a deep understanding of what the C ++ virtual base class is and how it runs.
As mentioned above, in order to initialize the sub-objects of the base class, the constructor of the derived class must call the constructor of the base class. For the virtual base class, because the object of the derived class has only one virtual base class sub-object. To ensure that the virtual base class sub-object is initialized only once, the virtual base class constructor must be called only once.
Because the hierarchy of the inheritance structure may be deep, it is required that the class specified when the object is created is called the most derived class. C ++ stipulates that the sub-objects of the virtual base class are initialized by the constructor of the most derived class by calling the constructor of the virtual base class. If a derived class has a direct or indirect C ++ virtual base class, the initial list of the constructor members of the derived class must list the calls to the virtual base class constructor. If not listed, it indicates that the default constructor of the virtual base class is used to initialize the virtual base class sub-objects in the object of the derived class.
The call of this virtual base class constructor must be listed in the member initialization list of the constructor In the derived class that is directly or indirectly inherited from the virtual base class. However, only the constructor of the most derived class used to create an object calls the constructor of the virtual base class.
The constructor calls to this virtual base class listed in the base class of the derived class are ignored during execution. This ensures that the object of the virtual base class is initialized only once. C ++ also stipulates that a call to the virtual base class and non-virtual base class constructor will appear in the member initialization list, then the constructor of the C ++ virtual base class is prior to the execution of the constructor of the non-virtual base class.
The following example describes how to use constructors of a derived class with a C ++ virtual base class.
- #include
- class A
- {
- public:
- A(const char *s) { cout< ~A() {}
- };
-
- class B : virtual public A
- {
- public:
- B(const char *s1, const char *s2):A(s1)
- {
- cout< }
- };
-
- class C : virtual public A
- {
- public:
- C(const char *s1, const char *s2):A(s1)
- {
- cout< }
- };
-
- class D : public B, public C
- {
- public:
- D(const char *s1, const char *s2, const char *s3, const char *s4)
- :B(s1, s2), C(s1, s3), A(s1)
- {
- cout< }
- };
-
- void main()
- {
- D *ptr = new D("class A", "class B", "class C", "class D");
- delete ptr;
- }
The C ++ virtual base class is used in the derived classes B and C, so that the created Class D object has only one virtual base class sub-object. The constructor Class A contains the constructor in the member initialization list of the constructor Class B, C, and D. When creating a class D object.
Only the virtual base class constructor listed in the initialization list of the constructor of the C ++ virtual base class D is called and called only once, the virtual base class constructor listed in the member initialization list of the Class D-based constructor is not executed. This can be seen from the output results of the program.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design