一、什么是this指针
struct Student
{
int a;
int b;
int c;
int d;
int Plus()
{
return a+b+c+d;
}
};
int main(int argc, char* argv[])
{
Student s={1,2,3,4};
int r = s.Plus; //会将1的地址当成参数传递
return 0;
}
this指针是编译器默认传入的,通常都会使用ecx进行参数的传递。
二、 this指针的使用
struct sclass
{
int a;
int b;
void Init(int a,int b)
{
this->a = a; //防止自己赋值给自己爆错
this->b = b;
}
void Print()
{
printf("%d %d",a,b);
}
};
int main(int argc, char* argv[])
{
return 0;
}
总结:
1、this指针是编译器默认传入的,通常都会使用ecx进行参数的传递。
2、你用或者不用,它都在那。
3、this指针不能做++ – 等运算,不能重新被赋值。
4、this指针不占用结构体的宽度。