我试图了解在此代码中返回参考的目的是什么?仅仅是因为返回参考比复制快得多还是有其他更重要的东西?
class Container
{
public:
int Elems;
int * data;
Container(int n):Elems(n){data=new int[Elems];}
Container operator= (const Container &rhs);
};
Container & Container:: operator= (const Container & rhs)
{// I deleted the & and I can still compiled and make such things (a=b=c)
if(this!=&rhs)
{
if(data!=NULL)
{
delete [] data;
}
Elems=rhs.Elems;
data=new int[Elems];
for(int i=0;i<Elems;i++)
data[i]=rhs.data[i];
}
return *this;
}