Back to Blog

【c++对象模型】

#C++ virtual

【C++ Object Model】

1. The overhead in C++ regarding layout and access time is primarily caused by virtual.

(1) virtual function, i.e., runtime binding;

(2) virtual base class, i.e., base class.

2. Table-Driven Object Model

(1) Each class generates a set of pointers to virtual functions, placed in a table, which is called the virtual table (Vtbl);

(2) Each class object is augmented with a pointer pointing to its associated virtual table. This pointer is commonly referred to as the vptr. The vptr's initialization and reset are automatically generated by each class's constructor, destructor, and copy assignment operator. The type_info object associated with each class (used to support runtime type identification, RTTI) is also pointed to via the virtual table, typically placed at the first slot of the table.

3. C++ supports polymorphism through the following mechanisms:

(1) Through a set of implicit conversion operations, such as converting a derived class to a pointer to its base type class. shape *ps = new circle();

(2) The virtual function mechanism;

(3) Through dynamic_cast and the typeid operator if (circle *pc = dynamic cast <circle*> (ps))...