1.个简单的例子:
class AA
{
int a;
int b;
void show(){cout < <a;cout < <b}
}
AA a;
a.show();
我的看法是 对象a在调用类的成员函数show()的时候 会把自己的“特权地址”作为参数“隐式”的传送给成员函数show()的隐藏形参this, 这样成员函数show()就能通过this指针来访问该对象的数据成员 a,b了
所以 void show(){cout < <a;cout < <b} 的真实形态应该是:
void show(AA*this){cout < <this-> a;cout < <this-> b}
2this在成员函数的开始前构造,在成员的结束后清除。
这个生命周期同任何一个函数的参数是一样的,没有任何区别。
因为,成员函数默认第一个参数就是this。
举例:
class A{
public:
int func(int p){}
};
func的原型在编译器看来,应该是
int func(A* const this, int p);