在类函数中,可以用this表示当前对象。
例如:
class test{
public:
test(int a,int b){
this->a=a;//this->a表示的是private中的a
this->b=b;
}
private:
int a;
int b;
}
但要注意的是,在全局函数、静态函数中无法使用this,只有在静态函数中使用。
this指针在类构造时被创建,在类被析构的时候销毁。
如果在类的成员函数右边加上const则表示修饰这个类的this对象,例如:
class test{
private:
int a;
int b;
int myfun() const{
this->a=a;//会报错,因为this对象在这个函数内是const的
this->b=b;
}
}