1. this指针只能在类的成员函数中调用,表示当前对象的地址;
void data::set_month(int mn)
{
month = mm;
this->month = mn;
(*this).month = mn;//三者等价
}
2. this指针只能在类的成员函数中使用,全局函数和静态函数都不能使用this;
实际上,类的成员函数默认的第一个参数为 T* const register this,
针对class A{public: int func(int p){}},
实际上func原型在编译器看来为: int func(A* const register this, int p);
3. this指针在类的成员函数开始之前构造,在类的成员结束后清除,这是this指针的生命周期。
当调用一个类的成员函数时,编译器将类的指针作为函数this参数传递过去,如:
A a;
a.func(10);
此时,编译器将会编译成:A::func(&a, 10);
编译器通常会对this指针做一些优化,因此,this指针的传递效率比较高,像VC是由ecx寄存器来传递this指针参数的。
4. this指针引编译器不同,放置的位置不同,可能是栈,可能是寄存器,甚至是全局变量;
5. this指针是通过类的成员函数的首参数来传递的;
6. 【注】
7. 资源下载链接:https://download.youkuaiyun.com/download/sss_369/10762986