[Turn] When exactly does the virtual table pointer (in C + +) gets set for a object?

Source: Internet
Author: User

Ps:http://stackoverflow.com/questions/7934540/when-exactly-does-the-virtual-table-pointer-in-c-gets-set-for-an-object

This is strictly implementation dependent.

For most compilers,

The compiler initializes this->__vptr within each constructor ' s Member Initializer list.

The idea-is-cause each object ' s v-pointer-to-point at its class ' s v-table, and the compiler generates the hidden code F Or this and adds it to the constructor code. Something like:

Base::Base(...arbitrary params...) : __vptr(&Base::__vtable[0]) ← supplied by the compiler, hidden from the programmer { }

This C + + FAQ explains a gist of what exactly happens.

This MSDN article explains it in great Detali

There it says:

"And the final answer is ... as you ' d expect. It happens in the constructor. "

If I might add, right on the beginning of the constructor, before any other code you might has in your constructor gets E Xecuted.


But was careful, let's say you had the Class A, and a class A1 derived from A.

    • If you create a new a object, the vptr'll be set right at the beginning of the constructor of the A class
    • If you create a new object A1:

"Here's the entire sequence of events when you construct an instance of class A1:

  1. A1::A1 calls A::a
  2. A::a sets vtable to A ' s vtable
  3. A::A executes and returns
  4. A1::A1 sets vtable to A1 ' s vtable
  5. A1::A1 executes and returns "

[Go] When exactly does the virtual table pointer (in C + +) gets set for a object?

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.