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:     }
        }
   
   

猜你喜欢

转载自www.cnblogs.com/huafan/p/11619445.html
006
今日推荐