Can constructor be a virtual function?

Source: Internet
Author: User

Constructor: allocates storage space for an object to initialize it;
Destructor: performs the final scanning at the end of the object's life cycle and releases the memory allocated by the constructor;

The constructor cannot be a virtual function:

Your own words: [virtual functions are meaningful only when the base class Pointer Points to a subclass object. When a base class Pointer Points to a subclass object, the subclass object has been constructed and there is no need for dynamic binding. Therefore, the virtual function cannot be a virtual function .]
In terms of concept, as mentioned above, the virtual function is valid only when applied to addresses, because the type information provided by addresses in the compilation phase is incomplete. The constructor function allocates space for an object in the memory. That is to say, the object type has been determined, and the compilation system knows exactly which class of constructor should be called, it is neither necessary nor possible to apply dynamic binding.
In terms of implementation, the vptr of each object needs to be initialized by the constructor (of course, it is automatically added by the compilation system.CodeBefore the constructor is called, vptr is not formed and dynamic binding is impossible.
What happens when there is a virtual function in the constructor? The result is that only the version of the virtual function in this class is called. That is to say, in the constructor, the virtual function mechanism does not work. Calling a virtual function is the same as calling a member function.

The Destructor can be a virtual function:
The Destructor can be a virtual function and should be declared as a virtual function. Similar to common member functions, when a destructor is called, the object structure has been completed, and vptr and vtable have been correctly initialized. Therefore, it is possible to implement a virtual destructor.
from the design point of view, the task of the Destructor is to release the memory, so it must know the type of the released object exactly; otherwise, useful data may be damaged, unpredictable consequences. For example, if we use a base class pointer to point to a derived class object, the storage space of the derived class object must be released when the memory is released. Therefore, destructor are often declared as virtual functions. For efficiency reasons, the default destructor is not a virtual function. However, as a practical experience, virtual destructor can be declared for each base class with virtual functions.
how does one work when there is a virtual function in the Destructor? Similar to the constructor, only the "local" version is called. However, the behavior is the same for different reasons. The constructor can only call the "local" version because the information of the derived class version is not available at the time of the call. The Destructor is because the information of the derived class version is no longer reliable. We know that the calling sequence of the Destructor is the opposite to that of the constructor, from the destructor of the derived class to the destructor of the base class. When a class destructor is called, the next level of the Destructor has been called, and the corresponding data has been lost, if you call the last version of the virtual function, it is equivalent to performing operations on some unreliable data, which is very dangerous. Therefore, the virtual function mechanism does not work in destructor.

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.