这个程序怎么理解
class CAT
{
public:
//......
CAT & operator=(const CAT &);
//......
private:
int *itsage;
}
CAT & CAT::operator=(const CAT &rhs)
{
if (this==&rhs)
return *this;
*itsage=rhs.getage();
return *this;
}
这两个return *this;分别是什么意思?
[解决办法]
this是一个指针 指向当前类对象
if (this==&rhs) //如果当前类对象与要等于的对象是同一个对象 直接返回当前对象
return *this;
*itsage=rhs.getage(); //如果当前类对象与要等于的对象不同 将当前类对象赋值 并返回当前类对象指针
return *this;