By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

A doubt on assignment operator.

P: n/a
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::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.

thanks,
--a.

Sep 26 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 26 Sep 2005 11:55:12 -0700
"Amit Bhatia" <am*********@gmail.com> wrote:
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::operator=(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::operator=(const Cell &r) const is certainly rubbish.

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpg http://gpg.notlong.com
Sep 26 '05 #2

P: n/a
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
Sep 26 '05 #3

P: n/a
On Mon, 26 Sep 2005 21:13:05 +0200
Moritz Beller <mo*****************@t-online.de> wrote:
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
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpg http://gpg.notlong.com
Sep 26 '05 #4

P: n/a

Victor Bazarov wrote:
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


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

Sep 27 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.