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

Source: Internet
Author: User

Summary

many textbooks have introduced to virtual pointers , virtual functions and virtual function tables, some say that class objects share a virtual function table, and some say that a class object has a virtual function table, and Some say, No matter how many class objects the user declares, this vtable virtual function table only one, there are said, Every object with a virtual function has a vptr virtual function pointer, which points to the first address of vtable, and each class has such a pointer to its object. Today, we will solve this problem, the same class of different objects, is not the "Same" virtual function table, the same is the same physical (memory address) or logically the same (data structure). This article is now detailed below!

" body "

virtual pointer: each containing virtual method (virtual function) There is a virtual table pointer in the object that points to the virtual table.

virtual function table: virtual function table is the order of the virtual function address, the virtual table is a sequential table, the tables stored 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 if 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, and then we can iterate over the function pointer and call the corresponding function.

"Code 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 the virtual function of different objects are equal, logically the same, and the addresses of the virtual functions in the class are stored;

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.