The reason why a C + + class constructor cannot be a virtual function

Source: Internet
Author: User

C + + Class object construction, the first application of a piece of memory, and then call the constructor to initialize;

We know that if there is a virtual function, there will be a virtual function table vtable, and the virtual function is when the expression is generated, of course, after the call to the constructor function;

So the problem is, if the constructor is a virtual function, at this time the memory is a blank, there is no such virtual function table vtable, then cannot find the constructor;

Therefore, the constructor cannot be a virtual function.


For destructors, a virtual function is possible because the virtual function table is already established, and often destructors are virtual functions. The reason is that, when destroying an object through a base-class pointer, you can correctly identify the type of object to be destroyed, thus preventing the destructor from appearing completely.


The constructor of a C + + class cannot be a reason for a virtual function

Related Article

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.