this指针

  1. this 指针是编译器默认传入的,指向当前结构体,使用ecx进行参数传递
  2. 用不用都在那
  3. this指针不支持++ —操作,不能重新赋值
  4. this 指针不占结构体宽度

赋值失败

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. struct Student
  5. {
  6. int a;
  7. int b;
  8. int c;
  9. int d;
  10. void init(int a,int b,int c,int d) {
  11. a = a;//会把a参数当成参数,赋值失败
  12. b = b;// 参数栈上自赋值
  13. c = c;
  14. d = d;
  15. }
  16. };
  17. int main(int argc, char* argv[]) {
  18. Student s;
  19. s.init(1,2,3,4);
  20. return 0;
  21. }
  1. s.init(1,2,3,4);
  2. 00E51802 push 4
  3. 00E51804 push 3
  4. 00E51806 push 2
  5. 00E51808 push 1
  6. 00E5180A lea ecx,[s] //this 指针,结构体
  7. 00E5180D call Student::init (0E51140h)
  8. return 0;
  9. struct Student
  10. {
  11. int a;
  12. int b;
  13. int c;
  14. int d;
  15. void init(int a,int b,int c,int d) {
  16. 00E51740 push ebp
  17. 00E51741 mov ebp,esp
  18. 00E51743 sub esp,0CCh
  19. 00E51749 push ebx
  20. 00E5174A push esi
  21. 00E5174B push edi
  22. 00E5174C push ecx
  23. 00E5174D lea edi,[ebp-0CCh]
  24. 00E51753 mov ecx,33h
  25. 00E51758 mov eax,0CCCCCCCCh
  26. 00E5175D rep stos dword ptr es:[edi]
  27. 00E5175F pop ecx
  28. 00E51760 mov dword ptr [this],ecx
  29. 00E51763 mov ecx,offset _A192CE94_源@cpp (0E5C015h)
  30. 00E51768 call @__CheckForDebuggerJustMyCode@4 (0E5130Ch)
  31. a = a;
  32. 00E5176D mov eax,dword ptr [a]
  33. 00E51770 mov dword ptr [a],eax
  34. b = b;
  35. 00E51773 mov eax,dword ptr [b]
  36. 00E51776 mov dword ptr [b],eax
  37. c = c;
  38. 00E51779 mov eax,dword ptr [c]
  39. 00E5177C mov dword ptr [c],eax
  40. d = d;
  41. 00E5177F mov eax,dword ptr [d]
  42. 00E51782 mov dword ptr [d],eax
  43. }

赋值成功

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Student
{
    int a;
    int b;
    int c;
    int d;
    void init(int a,int b,int c,int d) {
        this->a = a;//使用this 指针
        this->b = b;
        this->c = c;
        this->d = d;
    }
};

int main(int argc, char* argv[]) {
    Student s;
    s.init(1,2,3,4);
    return 0;
}
    Student s;
    s.init(1,2,3,4);
00D11802  push        4  
00D11804  push        3  
00D11806  push        2  
00D11808  push        1  
00D1180A  lea         ecx,[s]  
00D1180D  call        Student::init (0D11140h)  
    return 0;
00D11812  xor         eax,eax  
}


struct Student
{
    int a;
    int b;
    int c;
    int d;
    void init(int a,int b,int c,int d) {
00D11740  push        ebp  
00D11741  mov         ebp,esp  
00D11743  sub         esp,0CCh  
00D11749  push        ebx  
00D1174A  push        esi  
00D1174B  push        edi  
00D1174C  push        ecx  
00D1174D  lea         edi,[ebp-0CCh]  
00D11753  mov         ecx,33h  
00D11758  mov         eax,0CCCCCCCCh  
00D1175D  rep stos    dword ptr es:[edi]  
00D1175F  pop         ecx  
00D11760  mov         dword ptr [this],ecx  
00D11763  mov         ecx,offset _A192CE94_源@cpp (0D1C015h)  
00D11768  call        @__CheckForDebuggerJustMyCode@4 (0D1130Ch)  
        this->a = a;
00D1176D  mov         eax,dword ptr [this]  
00D11770  mov         ecx,dword ptr [a]  
00D11773  mov         dword ptr [eax],ecx  
        this->b = b;
00D11775  mov         eax,dword ptr [this]  
00D11778  mov         ecx,dword ptr [b]  
00D1177B  mov         dword ptr [eax+4],ecx  
        this->c = c;
00D1177E  mov         eax,dword ptr [this]  
00D11781  mov         ecx,dword ptr [c]  
00D11784  mov         dword ptr [eax+8],ecx  
        this->d = d;
00D11787  mov         eax,dword ptr [this]  
00D1178A  mov         ecx,dword ptr [d]  
00D1178D  mov         dword ptr [eax+0Ch],ecx  
    }

类继承

class SubClass : public SuperClass{……} class Pig : public Animal{….}