What was the time when the virtual function table in C + + was established in a previous blog post? , we know that the virtual function table was generated during compilation, but where is it placed in the executable file?
To solve this problem, let's look at the C + + object model first.
In the C + + object model, thenonstatic data members are configured in each class object, and the static data members are stored outside of the individual class object. The static and nonstatic function members are also placed outside of the individual class object. Virtual functions is supported in two steps:
1. Each class produces a bunch of pointers to virtual functions, which are placed in the table. This table is called Virtual table (VTBL).
2. Each class object is inserted with a pointer to the associated virtual table. Usually this pointer is called VPTR. the Vptr settings (setting) and reset (resetting) are automatically performed by each class's constructor, destructor, and copy assignment operators. each class-associated Type_info object (used to support runtime type identification, RTTI) is also pointed out via virtual talbe, usually placed in the first slot of the table.
We use a program to illustrate the C + + object model:
1 class Point2 {3 Public:4Point (floatxval);5 Virtual~Point ();6 7 floatX ()Const;8 Static intPointcount ();9 Ten protected: One Virtualostream& print (Ostream &os)Const; A floatx; - Static int_point_count; -}
The corresponding object model is:
One thing to note is that in most compilers, pointers to virtual function tables are usually placed at the front of the object, that is:
_vptr_point
Float_x
For the C + + object model, refer to a very good post-C + + object model.
In addition, from the C + + object model, we can know that the size of a class object is determined by the following three points:
1. The sum size of its nonstatic data members;
2. Add any space (padding) to be filled (possibly between members or at the aggregate boundary) due to alignment demand;
3. Add any additional burden (overhead) generated by the interior in order to support virtual.
Based on a blog post about the storage location of virtual function table in C + + analysis:
Under Linux, virtual function tables are stored in read-only data segments of the executable (rodata); Under Windows, virtual function tables are stored in a constant segment.
Resources:
In-depth exploration of the C + + object model
virtual function Miscellaneous