Why cannot the constructor be a virtual function?

Source: Internet
Author: User

1. From the perspective of storage space

Virtual functions correspond to a vtable. We all know that this 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 available and vtable cannot be found, therefore, the constructor cannot be a virtual function.

2. From the usage perspective

A virtual function is used to call a function that is overloaded with incomplete information. 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 pointer or reference of the base class in the lab. However, the analysis structure is not necessarily. We often destroy objects through the pointer of the base class. 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 perspective, vbtl is established after the constructor is called, so the constructor cannot become a virtual function.

In terms of actual meaning, the real type of the object cannot be determined when calling the constructor (because the child class calls the constructor of the parent class); and the role of the constructor is to provide initialization, an object is executed only once in its life cycle. It is not a dynamic behavior of the 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 v p t r. Therefore, it can only know that it is a "current" class, and ignore whether there is a successor behind this object. When the compiler generatesCodeIt generates code for the constructor of this class-neither the base class nor its derived class (because the class does not know who inherits it ).

Therefore, the V p t r used must be for the V ta B L E of this class. Furthermore, as long as it is the final constructor call, during the lifetime of this object, V p t r will be initialized to point to this V ta B L E, however, if another later-derived constructor is called, this constructor will set V p t r to point to its V ta B L E. until the final constructor ends. The status of V p t r is determined by the final called constructor. This is another reason why constructor calls are derived from the base class to the more class order.

However, when this series of constructor calls occur, each constructor has set V p t r to point to its own V ta B L E. If a function call uses a virtual mechanism, it will only generate a call through its own V ta B L E, instead of the final v ta B l e (only after all constructors are called will the final v ta B L E ).

 

Transferred from:

Http://blog.sina.com.cn/s/blog_620882f401016ri2.html

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.