this指针
- this 指针是编译器默认传入的,指向当前结构体,使用ecx进行参数传递
- 用不用都在那
- this指针不支持++ —操作,不能重新赋值
- this 指针不占结构体宽度
赋值失败
#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) {a = a;//会把a参数当成参数,赋值失败b = b;// 参数栈上自赋值c = c;d = d;}};int main(int argc, char* argv[]) {Student s;s.init(1,2,3,4);return 0;}
s.init(1,2,3,4);00E51802 push 400E51804 push 300E51806 push 200E51808 push 100E5180A lea ecx,[s] //this 指针,结构体00E5180D call Student::init (0E51140h)return 0;struct Student{int a;int b;int c;int d;void init(int a,int b,int c,int d) {00E51740 push ebp00E51741 mov ebp,esp00E51743 sub esp,0CCh00E51749 push ebx00E5174A push esi00E5174B push edi00E5174C push ecx00E5174D lea edi,[ebp-0CCh]00E51753 mov ecx,33h00E51758 mov eax,0CCCCCCCCh00E5175D rep stos dword ptr es:[edi]00E5175F pop ecx00E51760 mov dword ptr [this],ecx00E51763 mov ecx,offset _A192CE94_源@cpp (0E5C015h)00E51768 call @__CheckForDebuggerJustMyCode@4 (0E5130Ch)a = a;00E5176D mov eax,dword ptr [a]00E51770 mov dword ptr [a],eaxb = b;00E51773 mov eax,dword ptr [b]00E51776 mov dword ptr [b],eaxc = c;00E51779 mov eax,dword ptr [c]00E5177C mov dword ptr [c],eaxd = d;00E5177F mov eax,dword ptr [d]00E51782 mov dword ptr [d],eax}
赋值成功
#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{….}
