Why cannot constructor be a virtual function or a constructor of the base class?

Source: Internet
Author: User

Why can't constructors be virtual functions and basic class destructor?
I. Why cannot constructors be virtual functions?
1. From the perspective of storage space, virtual functions correspond to a pointer to the vtable virtual function table. We all know that this pointer to vtable is actually stored in the object's memory space. The problem arises. If the constructor is virtual, it needs to be called through vtable, but the object has not been instantiated, that is, the memory space is not yet available. How can we find vtable? Therefore, the constructor cannot be a virtual function. 2. From the perspective of usage, virtual functions are mainly used to call a function that can be reloaded if the information is incomplete. The constructor itself is to initialize the instance, so it does not actually make sense to use the virtual function. Therefore, the constructor does not need to be a virtual function. The role of a virtual function is to call the member function of the subclass when calling it through the pointer or reference of the parent class. The constructor is automatically called when an object is created. It cannot be called through the pointer or reference of the parent class. Therefore, the constructor cannot be a virtual function. 3. the constructor does not need to be a virtual function or a virtual function, because we always need to specify the object type when creating an object, although we may access it through the base class pointer or reference in the lab, the structure is not necessarily, we often destroy objects through the base class pointer. At this time, if the Destructor is not a virtual function, it cannot correctly identify the object type and thus cannot call the Destructor correctly. 4. from the implementation point of view, vbtl is established after the constructor is called, so the constructor cannot become a virtual function, the real type of the object cannot be determined when the constructor is called (because the child class calls the constructor of the parent class). Moreover, the constructor provides initialization and only executes the object once during its lifetime, it is not a dynamic behavior of an object, nor is it necessary to become a virtual function. 5. When a constructor is called, one of its first tasks is to initialize its VPTR. Therefore, it can only know that it is a "current" class, and ignore whether there is a successor behind this object. When the compiler generates code for this constructor, it generates code for the constructor of this class-neither the base class nor, it is not a derived class for it (because the class does not know who inherits it ). Therefore, the VPTR used must be a VTABLE for this class. In addition, as long as it is the final constructor call, during the lifetime of this object, VPTR will be initialized to point to this VTABLE, however, if another later-derived constructor is called, this constructor will set VPTR to point to its VTABLE. until the final constructor ends. The status of the VPTR is determined by the final called constructor. This is another reason why constructor calls are ordered from the base class to a more derived class. However, when this series of constructor calls occur, each constructor has set VPTR to point to its own VTABLE. If a function call uses a virtual mechanism, it will only generate a call through its own VTABLE, rather than the final VTABLE (the final VTABLE will be generated after all constructors are called ).

Compared with non-virtual functions, virtual functions have a cost.Memory and execution speedThere is a certain cost, including:

L each object will increase, increasing the number to store the pointer size of the virtual function table;

L for each class, the compiler creates a virtual function address table;

L for each function call, an additional operation is required, that is, to find the address in the virtual function table.

Although the efficiency of non-virtual functions is slightly higher than that of virtual functions, a single function does not support dynamic Association


2. Why is the destructor of the base class a virtual function?

When implementing polymorphism, when you use a base class to operate on a derived class, only the base class is not destructed.

Below

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.