C analysis series talks about Zheng Liqun
Foreword
The basic view of object-oriented programming is that the process is used to simulate the world, which makes it all kinds of fundamentals
Very very humanized, such as packages, inheritance, polymorphism, etc., and virtual functions are the maincoming of C
. In order to achieve polymorphism, the C compiler also revolutionizes the characteristics of dynamic networking (or night bundle).
.
The virtual function is also the key to the MFC programming. There are two main methods of MFC programming: First, respond to various messages
Perform a corresponding message processing. The second is to overload and rewrite the virtual function to realize some of your requirements or changes
Some default processing of the system.
The status of the virtual function is so important, and it is a poor root of it, and strives to know it and know it.
The improvement of our programming capacity is great. Let's listen to me.
Analysis of the implementation of polymorphism and dynamic cable
First, the base is omitted (limited to space, please refer to the corresponding C book):
1. Polymorphism: Use the basic class pointer to dynamically call the characteristics of the function in the derived class.
2, Dynamic Edge: In the operation phase, the call to the function is connected to the corresponding function body,
Calling or tie up at runtime.
Second, the process description:
1. The compiler found that there is a virtual function to generate a virtual function table for such a virtual function table vTable.
(Analysis of VTABLE). The various tables of the virtual function table are pointers that point to the corresponding virtual function.
2, the compiler implicit in this class into a pointer VPTR (for the VC compiler, it is inserted in the class
Position).
There is a way to let you perceive this implied pointer, although you can't see it directly in the class,
But you can compare the size of the class when you have a virtual function and the size of the class when there is no virtual function, you can
It is found that this pointer does exist.
Class cnovirtualfun {private: Long getmemberval ();} class chavevirtualfun {private: long lmember; public: virtual long getmembervalue ();
CNOVIRTUALFUN OBJ; SIZEOF (OBJ) -> == 4; chavevirtualfun obj; sizeof (obj) -> == 8;
3. When calling such constructor, in the constructor in the class, the compiler will impose VPTR and
VTABLE's related code, point VPTR to the corresponding VTABLE. This will contact this class with this type of VTable
Come.
4. When the class constructor is called, the pointer to the basic class has changed to the specific class.
The pointer, which relies on this this pointer to get the correct VTABLE, thereby achieving polymorphism. At this time
Really connected to the function body, this is dynamic cable. Third, VTABLE analysis:
Analysis 1: The virtual function table contains the address of all virtual functions of this class and its parent class. If it doesn't have a heavy duty
The virtual function of the class, the corresponding table item in the vTable points to this function of its parent class. Converse, pointing this function after heavy load
.
Analysis 2: The virtual function is still a virtual function after being inherited, and the virtual function is very strictly in order.
Sort in vtable, so the determined virtual function corresponds to a fixed position N, N is one in the vTable.
The constant determined when compiling. Therefore, using the VPTR plus the corresponding n, the entry address of the corresponding function can be obtained.
.
Fourth, the compiler calls the assembly code for the virtual function (refer to Think IN C ):
Push Funparam; first put the function parameters
Push Si; stack the THIS pointer to ensure operations on the current class
MOV BX, Word PTR [Si]; because the VC compiler places VPTR in the first location of the class, BX
VPTR
Call Word PTR [BX N]; call the virtual function. N = The virtual function called in correspondence in VTABLE
position
Pure virtual function:
I. Introduce the reason:
1. In order to facilitate the use of polymorphism, we often need to define virtual functions in the base class.
2, in many cases, the base class itself generates the object is unreasonable. For example, animals can be used as a base class
Subcords such as tigers and peacocks were derived, but the animal itself generated significantly uncommon.
In order to solve the above problem, the concept of pure virtual functions is introduced, and the function is defined as a pure virtual function (method:
Virtual Returntype function () = 0;), the compiler requires overloading in the derived class to achieve more
The state. At the same time, a class containing a pure virtual function is called an abstract class, which cannot generate an object. This is better to solve it.
Two problems mentioned above.
Second, the pure virtual function is essential:
1. The class contains a pure virtual function that its vTable table is not complete, there is a vacancy, so it cannot be generated
The image (the compiler is absolutely not allowed to call a possibility that there is no function). In its derived class, unless overloaded
This function, otherwise, this derived vTable table is incomplete, and it cannot generate an object, that is, it has become a
Pure vain class.
Virtual function and construct, destructor:
1. The constructor itself cannot be a virtual function; and the virtual mechanism does not work in the constructor (in the constructor
The virtual function in the number will only call a local version).
Think about it, use the virtual mechanism in the base class constructor, may be called to the subclass, at this time, the child is not yet
Cheng, what is the consequence! ? .
2, the destructor itself is often required to be a virtual function; but the virtual mechanism does not work in the destructor.
If the virtual function is used in the class, the destructor must be a virtual function, such as using a virtual mechanism.
DELETE, there is no virtual destructor, how can Delete's object you want Delete.
The virtual machine must not take effect in the destructive function, as it may cause the virtual to call the class that has been dropped by Delete.
The problem of functions.
Object slice:
When the back map (the subclass is mapped to the parent class), the VTABLE that occurs will occur completely into the parent class.
The situation of VTABLE. This is the object slice.
Cause: When the shot is mapped, the interface will narrow, and the compiler is absolutely not allowed to call a unssence letter.
The possibility of numbers, so the entrance to the newly derived virtual function in the subclass will be forced to "cut" in vtable, thus
The above is presented.
Disadvantages of virtual functions
The advantages have told a lot, now talk about the shortcomings, the most important disadvantage of virtual functions is lower, see one
See the cause of the polymorphism caused by virtual functions, you can experience the causes.