Constructor cannot be virtual function

巴扎黑
Release: 2016-12-20 16:48:59
Original
2379 people have browsed it

The reasons why constructors cannot be declared as virtual functions:

1. The so-called virtual function means that only one is executed in polymorphic situations. From the concept of inheritance, the parent class object is always constructed first, and then the subclass object can be used. If the constructor is set to a virtual function, then you have to explicitly call the constructor when constructing the constructor of the parent class. Another reason is to prevent errors from happening. Imagine if you accidentally override it in the subclass If you create a function that is the same as the parent class constructor, then your parent class constructor will be overwritten, that is, the construction of the parent class cannot be completed, and an error will occur. public

class B: public A

{
​​ public:
​​B( )
                                                                                                         Derived class inheritance results in polymorphism. In the constructor of the derived class, the compiler will add the code to construct the base class. If the constructor of the base class uses parameters, the derived class must provide parameters for the base class in its constructor. , this is the reason, the reason is a bit convoluted, that is, if the constructor of the derived class must be exactly the same as the parent class, this is obviously unrealistic

Of course there are other explanations: 1. From the perspective of storage space, virtual functions Corresponds to a vtable, but this vtable is actually stored in the memory space of the object. The problem arises. If the constructor is virtual, it needs to be called through the vtable, but the object has not been instantiated, that is, there is no memory space, and the vtable cannot be found, so the constructor cannot be a virtual function.

2. From the perspective of usage, virtual functions are mainly used to enable overloaded functions to be called accordingly when the information is incomplete. The constructor itself is to initialize the instance, so using virtual functions has no instance meaning. The first instance is not initialized, so how can it be polymorphic? The function of a virtual function is that when it is called through a pointer or reference of the parent class, it can become the member function that calls the subclass. The constructor is automatically called when an object is created, and cannot be called through a pointer or reference from the parent class. Therefore, it is stipulated that the constructor cannot be a virtual function.

3. From an implementation point of view, the vtable is created after the constructor is called, so the constructor cannot be a virtual function. From a practical point of view, the real type of the object cannot be determined when the constructor is called (subclasses need to call The constructor of the parent class), and the constructor provides initialization and is only executed once during the object's lifetime. It is not a dynamic behavior of the object, and there is no need to become a virtual function.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template