An analysis of the uniqueness of C + + on the virtual function table of class and Object

Source: Internet
Author: User

Summary

very much teaches The virtual pointers, virtual functions, and virtual function tables are introduced on the material. Some say that a class object shares a virtual function table, and some say that a class object has a virtual function table. Also No matter how many class objects the user declares, this vtable virtual function table has only one. Also some say, Every object with a virtual function has a vptr virtual function pointer, which points to the first address of vtable, and each class object has such a pointer. Today. We will solve the problem, the same class of different objects, is not the "Same" virtual function table, this is also physically the same (memory address) or logically the same (data structure). This article now details such as the following.

" body "

virtual pointer: each containing virtual method (virtual function) There are virtual table pointers in the object. Point to the virtual table.

Virtual function Table: virtual function table is the order of the virtual function address, virtual table is a sequential table, the table contains the virtual function address.


C + + compiler should be to ensure that pointers to virtual function tables exist in the first place in the object instance (this is to ensure the highest performance of the virtual function table-assuming there are multiple layers of inheritance or multiple inheritance).

This means that we get this virtual function table from the address of the object instance. You can then iterate through the function pointers and call the corresponding function.


"Code Demo sample"

#include <iostream>using namespace std; Class Base {public:virtual void F () {cout << "base::f" << Endl;} virtual void g () {cout << "base::g" << Endl;} virtual void H () {cout << "base::h" << Endl;}}; typedef void (*fun) (void); int main () {fun pfun = NULL; Base obj_1,obj_2;//obj_1 virtual function table-First function Pfun = (fun) * ((int*) * (int*) (&obj_1));p fun ();p fun = (fun) * (((int*) * (int*) ( &obj_1)) +1);p fun ();//Output virtual function table address and virtual function Table element value cout << "obj_1 virtual function Table address:" << (int*) (&obj_1) << Endl;cou T << "obj_2 virtual function Table address:" << (int*) (&obj_2) << endl;cout << "obj_1 virtual function table-First function address:" << (int*) * (int*) (&obj_1) << endl;cout << "obj_2 virtual function table-First function address:" << (int*) * (int*) (&obj_2) << Endl; return 0;} /* Output: base::f base::g obj_1 virtual function table address: 0018ff40 obj_2 virtual function table address: 0018ff3c obj _1 virtual function table-First function address: 0046f0ac obj_2 virtual function table-First function address: 0046F0AC Press any keyTo continue */ 

Conclusion

The elements in the table table of virtual functions of different objects are equal. The logical is the same, the store is the address of the virtual function in the class;

Different object virtual function table memory address is not the same, the physical is not the same, an object is a new table.

"Recommended"

See: C + + virtual function table parsing

Detailed Address: http://blog.csdn.net/haoel/article/details/1948051#comments

An analysis of the uniqueness of C + + on the virtual function table of class and 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.