A doubt on assignment operator.


A

Amit Bhatia

Hi,
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::eek:perator=(const Cell &r)
{
\\ all the stuff

}

and,

Cell& Cell::eek:perator=(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.

thanks,
--a.
 
Ad

Advertisements

M

Moritz Beller

Is the first work going to work at all?

Try it out for yourself.
does it make any sense?

Well, let's try and figure out what the definition const Cell&
Cell::eek:perator=(const Cell &r) actually means: It says that the
assignment operator shall return a constant reference of type Cell.
It's usually of no great importance whether you have both a const and
non-const assignment operator -- you will bearly use the object
returned by it.

It does however make sense for an (merely used) if-statement such as
if((Cell1=Cell2).changeContent("a"))
when you don't want Cell1's content to be changed.
I think it is not going to do what it is intended to do.

You don't mention what you intend to do, hence I can't tell you. FYI:
Cell& Cell::eek:perator=(const Cell &r) const is certainly rubbish.

best regards / Gruß
Moritz Beller
 
V

Victor Bazarov

Amit said:
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::eek:perator=(const Cell &r)
{
\\ all the stuff

}

and,

Cell& Cell::eek:perator=(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
 
M

Moritz Beller

It's usually of no great importance whether you have both a const and xx --> or
non-const assignment operator -- you will bearly use the object

Who put the "both ... and" in there? ;)

best regards / Gruß
Moritz Beller
 
Ad

Advertisements

G

Greg

Victor said:
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

The original poster probably can't tell whether either function works
because they won't compile. The two functions differ only by their
return type - making one an illegal overload of the other if both are
declared.

Greg
 

Top