c++中this怎么使用?请举例

来源:百度知道 编辑:UC知道 时间:2024/07/06 23:53:11

不用你显示调用.只要调用时提供对象名,系统回自动赋给函数一个对象的地址.给函数中的this指针使用

一般不需要使用this指针,只是在类中的成员函数返回自身引用或地址,以及判断对象地址时才使用,如:
class A
{
public:
A(int a):a_(a){}
Show()
{
cout<<a_<<endl;
//一般不使用 cout<<this->a_<<endl;
}
A& Me(){ return *this; }
void operator=(const A & a)
{
if(this != &a)
{
a_ = a.a_;
}
}
private:
int a_;
};