C + + Members do not use alternative (c-type) invocation of object invocation (. Or.)
#include <iostream>using namespacestd;/*we know that the biggest difference between the member function and the normal function is that the member function contains a hidden parameter this pointer, which indicates that the member function is currently acting on that object instance. Depending on the calling convention (calling convention), the member function implements the this pointer in a different way. 1. If you use the __thiscall calling convention, the this pointer is stored in the register ECX, which is the case with the VC compiler by default. 2. If it is a __stdcall or __cdecl calling convention, the this pointer is passed through the stack, and the this pointer is the last parameter to be pushed into the stack, which is equivalent to the compiler adding a This parameter to the leftmost parameter list of the function. */classBase { Public:Virtual voidF () {cout <<"base::f ()"<<Endl;}Virtual voidG () {cout <<"base::g ()"<<Endl;}Virtual voidH () {cout <<"base::h ()"<<Endl;}Virtual voidFoo (Base *pThis) {PThis->hello ( This); }Virtual voidHello (Base *pThis) {PThis-h ();}//virtual void __stdcall Hello (Base *pthis) {}//the member function specifies the __stdcall calling convention};intTest () {typedefvoid(*Fun ) (); Base*b =NewBase;cout<< * (int*) (&B) << Endl;//The address of the virtual function table is placed at the beginning of the objectFun funf= (Fun) (* (int*)*(int*) b); Fun Fung= (Fun) (* (int*)*(int*) B +1)); Fun FUNH= (Fun) (* (int*)*(int*) B +2));/************************************************************************//*calling an object method that has no members within this (including variables and methods)*//************************************************************************///if the following three methods are not used, the members associated with the object can not be assigned a value for ECX, otherwise an error will occurfunf (); Fung (); Funh ();/************************************************************************//*call an object method that has a member (including variables and methods) that has this participation inside*//************************************************************************///less __stdcall (note the position), the stack will be unbalanced: Originally C + + default is thiscall, if not, the VS compiler will let the caller balance stack, that is, an add ESP, 4typedefvoid(__stdcall *fun_base) (base*); Fun_base Foo= Fun_base (* (int*)*(int*) B +3));//is to add this sentence because the compiler uses ThisCall for C + + by default_asm{mov ecx, DWORD Ptr[b]}foo (b);//This is used in this, do not assign value to ECX, this is not right/************************************************************************//*invokes an object method that has a member (including variables and methods) that is involved in the internal, pure assembly version*//************************************************************************///the fourth method of attempting to invoke a virtual function table_asm{//Ditto, if the call method is not used, the member associated with the object can not be assigned a value for ECX, otherwise an error will occurmov ecx, DWORD ptr[b]push ecx//an incoming parameter//mov ecx, b or mov eax, [b], express the same meaning, vs finally are mov ecx, DWORD ptr[b];//The essence is mov eax, [ebp-04h], the result is ecx=b, that is, get the value of pointer b from the stack, not the value of *b (that is, (int*) b = = ptr_vftable). mov eax, [b]//Get Object Pointermov eax, [eax]//virtual function table first address, which is the beginning of the object, ptr_vftable = [eax + 0] = [ECX] = [This_of_b] = [b]Call [Eax +0x0c]//call the Fourth of a virtual function table, the Nth method (x86) in a virtual function list, [Ptr_vftable + (N-1) * 4] = [[Eax]+ (N-1)]}/************************************************************************//*a normal call*//************************************************************************//*00ef9886 8B F4 mov eax, DWORD ptr[b]00ef9889 push eax00ef988a 8B 4D F4 mov ecx, DWORD ptr[b]00ef988d 8B one mov E DX, DWORD ptr[ecx]00ef988f 8B 4D F4 mov ecx, DWORD ptr[b]00ef9892 8B 0C mov eax, DWORD Ptr[edx + 0ch]00ef9895 FF D0 cal l EAX*/b-foo (b);//http://blog.csdn.net/haoel/article/details/1948051//*The end node of a virtual function table, like the Terminator "/0" of a string, marks the end of a virtual function table. The value of this end flag is different under different compilers. 1. Under winxp+vs2003, this value is null. 2. In Ubuntu 7.10 + Linux 2.6.22 + GCC 4.1.3, 2.1 This value is if 1, indicating there is also the next virtual function table, 2.2 if the value is 0, represents the last virtual function table. */cout<< (Fun) (* (int*)*(int*) B +5));//The last position is 0, indicating the end of the virtual function tablereturn 0;}
Introduction to C + + virtual functions
1. First look at the virtual function table and polymorphic representation from C + + http://blog.csdn.net/haoel/article/details/1948051/
2. The size of a class with virtual functions http://blog.csdn.net/hackbuteer1/article/details/7883531
3. Again from the Assembly, the inheritance of the class, the formation of the virtual function table, the table is placed in which address and so on http://www.pediy.com/kssd/pediy10/60538.html
The formation of the virtual function table is implemented in the class constructor, which modifies the virtual function array stored in the first address of the instance object.
class Child: public base{public:virtualvoid vf1 () {cout<< " I ' m in sub Class. " ;} Virtual void vf2 () {cout<<"I ' m in sub Class. " ;} // ...}
. Text . Rdata?class Child Memory All objects in this class have a total of +-------------+ +-----+-----+-----+-----+----+| ptr_vftable |- ------> | Vf1 | VF2 | vf3 | ... | end?| +-------------+ +-----+-----+-----+-----+----+| || Other members| | | +-------------+
Reverse C + + (Chinese version)
Http://wenku.baidu.com/link?url=bjLVj2eqfe29_Edzi99MBGJeoCtVaHDXj-3r4s4lm771BAQnJ0WIUaQywPZgGq3Yz_ Uu9yh-b0v6q5sfmuhro0t436bunudahuhpwervlvc
C + + polymorphism:
Design patterns not mentioned in Gof's writings (4):D ouble Dispatch
Http://www.cnblogs.com/west-link/archive/2011/07/26/2116887.html
Http://en.wikipedia.org/wiki/Double_dispatch
C + + Members do not use direct invocation of object invocation notation