Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to construct a function for C++ virtual base class

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to construct the virtual base class of C++. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The following details introduce the C++ virtual base, the so-called C++ virtual base class: it is initialized by the constructor of the most derived class by calling the constructor 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 works.

As mentioned earlier, in order to initialize the children of the base class, the constructor of the derived class calls the constructor of the base class. For virtual base classes, because there is only one virtual base class sub-object in the object of the derived class. To ensure that the virtual base class child object is initialized only once, the virtual base class constructor must be called only once.

Because the hierarchy of the inheritance structure can be deep, it is specified that the class specified when the object is created is called the most derived class. C++ stipulates that the subobjects 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, calls to the virtual base class constructor must be listed in the member initial list of the derived class constructor. If not listed, the virtual base class child object in the derived class object is initialized using the default constructor of the virtual base class.

Calls to the constructor of the virtual base class are listed in the member initialization list of the constructor in the derived class that inherits directly or indirectly from the virtual base class. However, only the constructor of the most derived class used to create the object calls the constructor of the virtual base class.

The constructor calls to the virtual base class listed in the base class of the derived class are ignored in execution, which ensures that the object of the virtual base class is initialized only once. C++ also stipulates that if calls to the constructor of the virtual base class and the non-virtual base class appear in a member initialization list, the constructor of the C++ virtual base class precedes the execution of the constructor of the non-virtual base class.

The following is an example of the use of the constructor 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 * S2, const char * S4): B (S1, S2), C (S1, S2), A (S1) {cout <}; void main () {D * ptr = new D ("class A", "class B", "class C", "class D"); delete ptr;}

C++ virtual base class is used in derived classes B and C, so that the established class D object has only one virtual base class sub-object. The constructor for the virtual base class An is included in the member initialization list of the constructor of the derived class B ~ C ~ ~ D. When creating a D-like object.

Only the virtual base class constructor listed in the member initialization list of the C++ virtual base class D is called, and only once, while the virtual base class constructor listed in the member initialization list of the base class D is not executed. This can be seen from the output of the program.

On how to share the C++ virtual base class constructor here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report