First, introduce
Defines the object of a class, first the system has allocated space to the object, and then calls the constructor.
A class has multiple objects, and it is possible to access a member variable of that object when a function of the object is called in the program.
For each object of the same class, the same class function is shared. An object has a separate variable, but there is no separate function, so when calling a function, the system must let the function know which object is being manipulated to determine which object the member variable is.
This is called the This pointer, which is used to differentiate a member variable from belonging to the image. In fact it is the address of the object, which can be seen from the disassembled code.
Second, analysis
1. Test code:
Copy Code code as follows:
/////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace Std;
/////////////////////////////////////////////////////
Class A
{
Public
A (char *szname)
{
cout<< "Construct" <<endl;
Name
= new CHAR[20];
strcpy (Name,
szname);
}
~a ()
{
cout<< "Destruct" <<endl;
Delete name;
}
void Show ();
Private
Char *name;
};
/////////////////////////////////////////////////////
void A::show ()
{
cout<< "Name
= "<<name<<endl;
}
/////////////////////////////////////////////////////
int main ()
{
A
A ("Zhangsan");
A.show ();
System ("pause");
return 0;
}
The program compiles and runs on the vc++6.0 32-bit operating system.
Disassemble the compiled EXE file. The Disassembly tool is ollydbg.
2. Disassembly analysis
The key points screenshot is as follows:
(1) You can see from figure 1 that this pointer passes through the ECX register to the member function. The this pointer is the address of the object.
Figure 1 Main function
(2) You can see from Figure 2 that accessing the member variable of an object is using the this pointer passed in before the ECX.
Figure 2 the Show () function
Third, in-depth understanding
With screenshots and related data, it is clear that the ECX is the this pointer before calling the constructor, show () function, which means that this is a validated experiment, and the answer is clear enough to try to experience it. However, if I do not understand C + +, I do not know what this pointer, I can find this is called the "this pointer" thing. With the dynamic debugging of OD, when name is displayed, a step-by-step backtracking can find that the source of name is ECX. OD reload to see where Ecx came before entering the show () function, and finally, the ECX is an address, and the first value in this address is also an address, pointing to a string of strings. Further analysis, enter show () above the constructor, you can find that there is a new operation, strcpy operation, where the string space, the source of content. At this point, the basic analysis is over.
Through this process can find a lot of C + + knowledge. For example, the space of an object is allocated before the constructor is invoked ; the object has no function ; this pointer passes through the register ECX; its space is allocated to the stack by the declared object. , and so on, the knowledge that is associated with the system or C + +.
However, for a person who does not understand C + +, the above paragraph of experience is not. I can't see the idea of C + + from the assembly instruction, this pointer is just an address; objects are just spaces; constructors, destructors, and other functions are just a collection of instructions.
The multiple objects defined by the same class of C + +, from the assembly instructions appear to be: There are many block address spaces, they have the same size. When a different object invokes a member function, the assembly instructions appear to be: they all call the same address, and the call instruction is actually a jmp instruction, which is used to jump to a location, usually placing an address in the ECX before the command, and of course sometimes using stacks or other registers.
C + + inheritance, polymorphism, encapsulation, for assembly programmers is not see what magic, for C + + programmers that can be different, can save a lot of work, put a lot of things to the compiler , Let the compiler automatically fix it for you.
The objects discussed by C + + programmers and their many features and benefits eventually turn into "low-level" directives, and may be inefficient directives, even though their advantages are far greater than disadvantages, making programming easy and efficient.
iv. Extension
Suddenly thought of C + + polymorphism, a phrase "assign a pointer to a subclass type to a pointer to the parent class type," polymorphism is implemented through a virtual function. The principle of the virtual function and its related content, the detailed understanding will not elaborate.
My simple understanding is that there is a base class A and subclass B, C, there is a function with a pointer to the base class A parameter, and then in the function through the pointer to call the base class member function. If this called base-class member function is not a virtual function, then it is not possible to achieve polymorphism, because when translating into assembly instructions, the place where the calling function is called is a call instruction, and then this call instruction jumps somewhere to execute, which is a fixed address. By defining it as a virtual function, this place where the member function is invoked is determined by the virtual function table pointer, and the virtual function table pointer is placed in the object's address space, and if the object changes, the virtual function table pointer changes, and the function of the call is different. For a function with a pointer to the base class A, the pointer is the address of the object, and if the address that is passed is the address of the object of subclass B or C, then the virtual function table pointer is different and the member function of the invocation is different.
This is polymorphism, which makes it possible to invoke the same function because of the difference between passing arguments, which can be base class objects or many different subclass objects. Their differences are between class and class.
The memory layout of an object with a virtual function has a pointer to a virtual function table more than an object without a virtual function. Because the invocation of the virtual function is implemented through the virtual function table pointer, there is polymorphism.
consider the this pointer for C + +, a member function in a class that distinguishes a different object by the this pointer, which means according to this pointer Implements a member variable that accesses a different object.
is this also a manifestation of polymorphism? The polymorphism described here is not the dogma of the "parent pointer to a subclass object", but rather the same as the between different objects of the same class , and calls the same member function , which implements a member variable that accesses a different object, based on the parameter "this pointer." A member function accesses a member variable that cannot be determined at the compile time at which address of the member variable it accesses, only to the run time by this pointer to determine the address of the access . This is very similar to the polymorphism of a class: a virtual member function of a base class is called in a function that takes a base class pointer as a parameter, and it is not possible at compile time to determine which class object The program is calling at runtime, only to determine which class's object will be invoked until the runtime. The
This pointer recognizes different objects of the same class, in other words, the this pointer makes it possible for a member function to access different objects of the same class. Further, the this pointer causes the member function to access different member variables because the this pointer is different. This is a polymorphic bar, but it is the inevitable polymorphism, the polymorphism between the base class and the derived class is different levels of polymorphism, it is not like the general polymorphism can be chosen by the use of virtual functions, it is a class corresponding to multiple objects, multiple objects share a member function code to bring the inevitable result.