求助:关于This指针?
this指针的作用是什么啊?在哪些情况下可以用?可以举个例子吗?谢谢!
this指针的作用是什么啊?在哪些情况下可以用?可以举个例子吗?谢谢!
例子:+ 法重载
[CODE]class complex
{
private:
double real,imag;
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
complex operator +(complex c2);
double outputreal(){return real;}
double outputimag(){return imag;}
};
complex complex::operator +(complex c2)
{
return complex(this->real+c2.real,this->imag+c2.imag); //注意这里的this的使用
}
void main()
{
complex a(3.2,4),b(2.3,5);
complex c=a+b;
cout<<"a:"<<a.outputreal()<<"+"<<a.outputimag()<<"i"<<endl;
cout<<"b:"<<b.outputreal()<<"+"<<b.outputimag()<<"i"<<endl;
cout<<"a+b="<<c.outputreal()<<"+"<<c.outputimag()<<"i"<<endl;
}[/CODE]