【c++对象模型】
Understanding the C++ Object Model
In this post, we will delve into the C++ object model, focusing on the implications of using virtual functions and how they facilitate polymorphism. By the end of this article, you will gain insights into the overhead introduced by virtual functions, the structure of the virtual table (Vtbl), and the mechanisms that enable polymorphism in C++.
1. Overhead in C++ Due to Virtual Functions
The primary source of overhead in C++ regarding layout and access time is due to the use of virtual functions. There are two key aspects to consider:
-
Virtual Function: This refers to runtime binding, where the function to be executed is determined at runtime rather than compile time. This allows for more dynamic behavior in object-oriented programming but introduces additional overhead due to the need for runtime lookups.
-
Virtual Base Class: This is a base class that is declared as virtual. It ensures that only one instance of the base class is included in the inheritance hierarchy when multiple derived classes inherit from it. This is particularly important in cases of multiple inheritance.
2. The Table-Driven Object Model
C++ employs a table-driven object model to manage virtual functions efficiently. Here’s how it works:
-
Virtual Table (Vtbl): Each class that contains virtual functions generates a virtual table, which is a collection of pointers to the virtual functions defined in that class. This table is crucial for enabling dynamic dispatch of function calls.
-
Virtual Pointer (vptr): Each object of a class with virtual functions contains a pointer, known as the vptr, which points to the corresponding virtual table. The initialization and management of the vptr are handled automatically by the class's constructor, destructor, and copy assignment operator.
Additionally, the
type_infoobject associated with each class, which supports runtime type identification (RTTI), is also referenced through the virtual table. Typically, thistype_infoobject is stored in the first slot of the virtual table.
3. Mechanisms Supporting Polymorphism in C++
C++ supports polymorphism through several mechanisms, which allow for flexibility and dynamic behavior in object-oriented programming:
-
Implicit Conversion Operations: C++ allows for implicit conversion of derived class pointers to base class pointers. For example, you can create a pointer to a base class type that points to an object of a derived class:
shape *ps = new circle(); -
Virtual Function Mechanism: The use of virtual functions enables derived classes to override base class functions, allowing for dynamic behavior based on the actual object type at runtime.
-
Dynamic Casting and Type Identification: C++ provides
dynamic_castfor safe downcasting between types in an inheritance hierarchy. This allows you to check the type of an object at runtime:if (circle *pc = dynamic_cast<circle*>(ps)) { // Successfully casted to circle }
In conclusion, understanding the C++ object model is essential for effective programming in C++. The overhead introduced by virtual functions, the structure of the virtual table, and the mechanisms for polymorphism are foundational concepts that every C++ developer should grasp to write efficient and maintainable code.