Returning by reference
Code:
class Foo
{
int data;
public:
Foo (int n):data (n)
{
}
Foo ():data (0)
{
}
Foo &operator= (int n)
{
data = n;
return *this;
}
Foo &operator= (const Foo& foo)
{
data = foo.data;
return *this;
}
void display ()
{
cout << "data = " << data << endl;
}
};
Why in the above code the operator= returns by reference? If I return it by value (Foo operator= ()) it seems to work fine. Do we return by refernce because it is faster (only the address of the object will be returned instead of making a copy of the object and then returning it, please correct me if I'm wrong
)?
Thanks.
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!