Amit Bhatia wrote:
I am trying to define the assignment operator for a class called
Cell. I am bit confused between the following two statements:
const Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff
}
and,
Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff
}
Is the first work going to work at all? does it make any sense? I think
it is not going to do what it is intended to do.
The difference between the two is the return value type, right? The
return value type is different only in the const-ness of the object to
which the returned reference points, right? So, why would the first
one *not work*? When you write the expression
someCell = someOtherCell;
it's the same as
someCell.operator=(someOtherCell);
which basically _discards_ the return value. Whatever happens between
passing the argument and returning a value, IOW, whatever happens between
the curly braces of the function, is the same, no? So, how it may not
work if they are doing exactly the same?
V