The this pointer of C ++ and another "polymorphism"
2010-7-9
Candle fall
I,Introduction
To define an object of a class, the system first allocates space for the object, and then calls the constructor (Note: Suppose there is a constructor -- 2010.9.5 ). A class has multiple objects.ProgramWhen calling a function of an object, it is possible to access the member variables of this object. For each object of the same class, the same class function is shared. Objects have independent variables, but they do not have independent functions. Therefore, when calling a function, the system must let the function know the operation of the object to determine which object the member variable belongs. This is called the this pointer. In fact, it is the object address.CodeYou can see.
II,Analysis
1. Test code:
//////////////////////////////////////// //////////////////////////////////////// ///// # Include <iostream> usingnamespacestd; //////////////////////////////////////// //// // Class A {public: A (char * szname) {cout <"construct" <Endl; name = new char [20]; strcpy (name, szname );}~ A () {cout <"destruct" <Endl; Delete name;} voidshow (); Private: char * Name ;}; //////////////////////////////////////// //// // voida:: Show () {cout <"name =" <name <Endl ;} //////////////////////////////////////// /// // intmain () {A ("zhangsan");. show (); System ("pause"); Return 0 ;}
Programs are compiled and run on the VC ++ 6.0 32-bit operating system.
Decompile the compiled EXE file. The Disassembly tool is ollydbg.
2. disassembly Analysis
The key points are as follows:
(1) Figure 1 shows that this pointer is passed to the member function through the ECX register. This pointer is the object address.
Figure 1 Main Function
(2) From figure 2, we can see that the member variables of the Access Object use the this pointer passed in through ECx.
Figure 2 show () function
Iii. In-depth understanding
Through relevant information, we can clearly know that the ECX before calling constructor and show () function is the this pointer, that is, this is a validation experiment, the answer is clear. All you need to do is try it out. However, if I do not understand C ++ or this pointer, I can also find this "This pointer. Through the dynamic debugging of OD, when the name is displayed, the source of the name is ECx. Od re-load, and check where ECx came before entering the show () function. In the end, we can step by step find that ECx is an address, and the first value in this address is also an address, point to a string. Next, go to the show () constructor and find the new and strcpy operations in the constructor. Here we find the source of the string space and content. Now, the analysis is complete.
Through this process, we can find a lot of C ++ knowledge. For example, the object space is allocated before calling the constructor; there is no function in the object; this pointer is passed through the Register ECx; the space of the object defined by declaration is allocated to the stack; and so on, the knowledge associated with the system or C ++.
However, for a person who does not understand C ++, there is no experience in the above section. The idea of C ++ cannot be seen from assembly instructions. This pointer is just an address; the object is just some space; constructor, destructor, and other functions, it is just a collection of commands. The same class of C ++ defines multiple objects. From the perspective of Assembly commands, there are many block address spaces with the same size. When different objects call member functions, in the Assembly command view, they call the same address. This call command is actually a JMP command used to jump to a certain position, an address is usually put into ECx before the call command, and sometimes the stack or other registers are used. The inheritance, polymorphism, and encapsulation of C ++ cannot be seen by compilation programmers. It can be different for C ++ programmers, which can save a lot of work, I handed a lot of things to the compiler and let the compiler automatically handle them for you. The objects discussed by C ++ programmers and their many characteristics and advantages eventually become "low-level" commands, and may be inefficient commands. Even so, its advantages are far greater than its disadvantages, making programming easier and more efficient.
Iv. Extension
Suddenly thought of the C ++ polymorphism, a sentence "assign a pointer of the subclass type to the pointer of the parent class", polymorphism is achieved through virtual functions. I will not elaborate on the principles and detailed understanding of virtual functions and their related content. In my simple understanding, there is a base class A, a subclass B, and C, and a function takes the pointer of base class A as the parameter, then, call the member functions of the base class through pointers in the function. If the called base-class member function is not a virtual function, it is impossible to implement polymorphism, because when it is translated into an assembly command, the call command is called, then the call command jumps to a certain place for execution, which is a fixed address. By defining it as a virtual function, the virtual function table pointer is used to determine which function to call. The virtual function table pointer is placed in the address space of the object, if the object changes, the virtual function table pointer also changes, and the called function is different. For a function that takes the pointer of base class A as the parameter, the pointer is the address of the object. If the transmitted address is the address of the object of subclass B or C, then the table pointer of the virtual function is different, and the called member functions are different.
This is polymorphism. This polymorphism makes it possible to call the same function. Because different parameters are passed, the parameter can be a base class object or many different subclass objects. Their differences are between classes.
The memory layout of objects with virtual functions provides a pointer to the virtual function table more than that of objects without virtual functions. Because the virtual function is called through the virtual function table pointer, There Is A polymorphism. Consider the this pointer of C ++, a member function in a class, to distinguish different objects based on the this pointer. That is to say, the member variables used to access different objects are implemented based on the this pointer.
Is this also a manifestation of polymorphism? The polymorphism mentioned here is no longer the dogma of "parent class pointer pointing to subclass object", but is embodied in calling the same member function between different objects of the same class, the parameter "This pointer" is used to access member variables of different objects. When a member function accesses a member variable, it cannot determine the address of the member variable it accesses during the compilation period. Only when this pointer is reached can the access address be determined. This is similar to the class polymorphism: the function that uses the base class pointer as the parameter calls the virtual member function of a base class, during the compilation period, you cannot determine the class object to be called when the program runs. Only at runtime can you determine the class object to be called.
This pointer identifies different objects of the same class. In other words, this pointer allows member functions to access different objects of the same class. Further down, this pointer allows the member function to access different member variables due to the difference of this pointer. This is also a polymorphism, but it is an inevitable polymorphism. The polymorphism between the polymorphism and the base class and the derived class is a different level of polymorphism, unlike General polymorphism, you can choose between virtual functions. A Class corresponds to multiple objects and multiple objects share a member function code.