Virtual function table

Source: Internet
Author: User

The virtual function table completes the implementation of virtual functions by the C ++ compiler, that is, dynamic binding. To achieve dynamic binding (later binding), the C ++ compiler uses a table, during the re-execution period, "indirectly" calls the function actually to be bound. Such a table becomes a virtual function table vtable)
For each "class containing virtual functions", the compiler creates a virtual function table for it. Each element in the table points to the address of a virtual function.

In addition, the compiler will add a member variable to the class, which is a pointer to the virtual function (vptr), each class containing the virtual function, derived objects, there is such a vptr. When we call a virtual function with this object, we actually find the virtual function table through vptr and then find the real address of the virtual function.

The content of the virtual function table is filled with function pointers one by one based on the virtual function declaration order in the class. The derived class will inherit the virtual function table of the base class (and all other Members that can be inherited). When we rewrite the virtual function in the derived class, the virtual function table will be affected: the function address referred to by the element in the table is no longer the function address of the base class, but the function address of the derived class.

Author: "thousands of miles"

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.