1.this指针的作用
非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针。
class complex
{
public:
double real,imag;
void print(){cout<<real<<","<<imag;}
complex(double r,double i){real=r;imag=i}
complex addone(){
this->real++;
this->print();
return *this;
}
};
int main()
{
complex c1(1,1),c2(0,0);
c2=c1.addone();
return 0;
}
class a
{
int i;
public:
void hello(){cout<<"hello"<<endl;}//——>void hello(a *this){cout<<"hello"<<endl;}
};
int main()
{
a *p=NULL;
p->hello(); //->hello(p);
}
//输出:hello