C ++ object-oriented model

xiaoxiao2021-03-05  26

Abstract: The main feature of the C object-oriented model is: (1) Class and encapsulation (2) inheritance (3) polymorphism. The polymorphism is implemented on the basis of the former and virtual functions, and the virtual function is the core mechanism for object-oriented. Keywords: virtual function inheritance polymorphism

In object-oriented programming, the system is considered to be composed of multiple objects, and the system is formed by communication between the objects. Its main feature is: (1) Class and encapsulation (2) inheritance (3) polymorphism. The polymorphism is implemented on the basis of the former and virtual functions, and the virtual function is the core mechanism for object-oriented.

1. Virtual function and VPTR, VTBL in C , there are two data members: static and nonStatic, there are three member functions: static functions, non-static functions, and virtual functions. In the C model designed in Stroustrup, non-static data members are stored within each object, and static data members are stored outside of all objects. Static member functions and non-static member functions are also stored outside of all objects. The virtual function is supported in two steps:

Each class produces a team that points to the virtual function, placed in a table. This table is called a virtual function table (VTBL). Each object adds a pointer to the compiler, pointing to the related virtual function table. Usually this pointer is called VPTR. VPTR Settings and Reset (RESETTING) Automatically completes each class's constructor, a destructor, and a copy assignment operator. The type_info object associated with each class is also indicated by the virtual function table, usually in the first record of the virtual function table through the virtual function table.

For example, Class A is defined as follows:

Class a {

Private:

Int value;

PUBLIC:

Virtual Void Func1 (Void)

Virtual Void Func2 (Void)

}

The memory layout of Class A is as follows: (for simplified instructions, slightly Type_info Object in the virtual function table)

Figure 2-1 Object memory diagram of class A

2. Inheritance and polymorphism

Class B inherits to Class A, defined as follows:

Class B: PULIC A {

Private:

Int value1;

PUBLIC:

Virtual Void Func1 (Void)

Virtual Void Func2 (Void)

}

The memory layout of Class B is as follows:

Figure 2-2 Object memory diagram of class B

Assume that there is the following code:

A Objecta;

B Objectb;

A * PA = & objecta;

PA-> FUNC1 ();

PA-> Func2 ();

PA = & objectb;

PA-> FUNC1 ();

PA-> Func2 ();

The first PA-> FUNC1 () statement actually calls A :: Func1 (), the compiler will convert it to (* PA-> VPTR [1]) (this), PA-> VPTR [1] A :: func1, and the second PA-> FUNC1 () statement calls b :: func1 (), the compiler also converts it to (* PA-> VPTR [1]) (this), just at this time PA- > VPTR [1] has been associated with B :: Func1. The first PA-> FUNC2 () statement actually calls A :: Func2 (), the compiler converts it to (* PA-> VPTR [2]) (this), PA-> VPTR [2] is A :: FUNC2, and the second PA-> FUNC2 () statement calls B :: Func2 (). On the basis of inheritance and virtual functions, C implements a polymorphism.

3. Multiple inheritance

Class C, Class D, Class E are defined as follows:

Class C {

Private:

Int value2;

PUBLIC:

Virtual Void Func3 (Void)

Virtual Void Func4 (Void)

}

Class D: public c {

Private:

Int value3;

PUBLIC:

Virtual Void Func3 (Void)

}

Class E: Public B, Public D {PUBLIC D {

Private:

Int value4;

PUBLIC:

Virtual Void Func1 (Void)

Virtual Void Func4 (Void)

}

The relationship between Class A, Class B, Class C, Class D, Class E is shown in the figure:

Figure 2-3 Inheritance Relationship of Class A, B, C, D, E

Figure 2-4 Object memory diagram of class E

There are two VPTR in the memory layout of the class E object, the first derived self-class B, the second derived self-class D. Call the virtual function of the derived class by pointing to the pointer to the second or subsequent base class, you need to adjust the THIS pointer at the appropriate offset value (OFFSET) to make the pointer to the object or class D of the class C. Correctly call D :: Func3 and E :: Func4.

The more efficient solution is to use Thunk technology, thunk is a small assembly code for adjusting the THIS pointer with an appropriate offset value and jumps to the corresponding virtual function. For example, the func4 (PD-> FUNC4) is called by a pointer to the object to the class D, and the associated THUNK class C code is as follows:

THIS = SIZEOF (B);

E :: Fun4 (this);

reference:

Pan Aimin COM

Stanley Lippman, "INSIDE C Object Model"

转载请注明原文地址:https://www.9cbs.com/read-36116.html

New Post(0)