006 this指针原理

Posted huafan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了006 this指针原理相关的知识,希望对你有一定的参考价值。

 

/*
目录:
   一 this指针原理
*/

 

一 this指针原理

class CTest

public:
    CTest(int nNum)
    
        this->nNum = nNum;
    
    ~CTest()
    

    
    void Print()
    
        cout << nNum << endl;
    
private:
    int nNum;
;


int main(int argc, char *argv[], char **envp)

    CTest c(0x11);
    c.Print();

    return 0;


// 反汇编
// 调用函数
int main(int argc, char *argv[], char **envp)

   136:     CTest c(0x11);
0041A8BD  push        11h  
0041A8BF  lea         ecx,[c]                      // 获取地址 : ecx  - 变量c地址
0041A8C2  call        CTest::CTest (0411983h)  
0041A8C7  mov         dword ptr [ebp-4],0  
    return 0;



// 别调函数
   114: class CTest
   115: 
   116: public:
   117:     CTest(int nNum)
004122F0  push        ebp  
004122F1  mov         ebp,esp  
004122F3  sub         esp,0CCh  
004122F9  push        ebx  
004122FA  push        esi  
004122FB  push        edi  
004122FC  push        ecx                      // 压栈参数 : ecx - 变量c地址
004122FD  lea         edi,[ebp-0CCh]  
00412303  mov         ecx,33h  
00412308  mov         eax,0CCCCCCCCh  
0041230D  rep stos    dword ptr es:[edi]  
0041230F  pop         ecx                      // 出栈参数 : ecx - 变量c地址    
00412310  mov         dword ptr [this],ecx  // 参数赋值 : ecx - 隐藏变量this指针; 此时this指针指向mian函数中变量c地址。
   118:     
   119:         this->nNum = nNum;
00412313  mov         eax,dword ptr [this]  
00412316  mov         ecx,dword ptr [nNum]  
00412319  mov         dword ptr [eax],ecx  
   120:     
        
   
   

 

以上是关于006 this指针原理的主要内容,如果未能解决你的问题,请参考以下文章

快慢指针原理--快速找到未知长度单链表的中间节点

C++中怎么获取类的成员函数的函数指针

C++多态与this指针问题

this指针

C++类体系中this指针不能改变指向吗?

this指针是指向虚函数表的指针