468,771 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

confusion with const functions return types

Hi, I am a little confused with const functions, for instance (here i am
just overloading the unary+ operator) if i define:

1) Length Length :: operator+ ( void ) const {return * this;}

... I get no error...

2) Length & Length :: operator+ ( void ) const {return * this;}

... I get the error -> 'return' : cannot convert from 'const class Length' to
'class Length &'

3) const Length & Length :: operator+ ( void ) const {return * this;}

... I get no error

----------My Initial line of thinking---

Now 1) returns the actual '*this' object back, and 3) returns a reference to
the '*this' object. Why is it when we return a reference that we have to
return it as a const reference (i.e. 'const' out the front as in 3) ...
whereas when we return the object itself, it doesnt have to be returned as a
const object as in 1) ...... In other words, if the returned object doesnt
have to be returned as a const object, then the returned reference to that
object shouldnt have to be returned as a const object... so why doesnt 2)
work?

Ok.. so i am looking at this another way..

----------My Alternative line of thinking---

I know that in 1) the const specifies that the *this object is not
modifiable within the context of the member function - right? So doesnt it
mean that if I returh the *this object, I have to return it as a const
object? So By definition isnt 1) logically wrong... shouldnt it be written:

1) const Length Length :: operator+ ( void ) const {return * this;}

(actually I have tried this and I get no error...)

.... hence this line of thinking would explain why 2) is wrong and 3) is
right.

I am confused :)

Any help? thanks!
Zork
Jul 22 '05 #1
3 1395
"Zork" <Zo**@z.com> wrote in
news:13******************************@news.teranew s.com:
Hi, I am a little confused with const functions, for instance (here i
am just overloading the unary+ operator) if i define:

1) Length Length :: operator+ ( void ) const {return * this;}

.. I get no error...

2) Length & Length :: operator+ ( void ) const {return * this;}

.. I get the error -> 'return' : cannot convert from 'const class
Length' to 'class Length &'

3) const Length & Length :: operator+ ( void ) const {return * this;}

.. I get no error

----------My Initial line of thinking---

Now 1) returns the actual '*this' object back,
and 3) returns a reference to the '*this' object. Why is it when we return a reference
that we have to return it as a const reference (i.e. 'const' out the
front as in 3) ... whereas when we return the object itself, it doesnt
have to be returned as a const object as in 1) ...... In other words,
if the returned object doesnt have to be returned as a const object,
then the returned reference to that object shouldnt have to be
returned as a const object... so why doesnt 2) work?

Ok.. so i am looking at this another way..

----------My Alternative line of thinking---

I know that in 1) the const specifies that the *this object is not
modifiable within the context of the member function - right? So
doesnt it mean that if I returh the *this object, I have to return it
as a const object? So By definition isnt 1) logically wrong...
shouldnt it be written:

1) const Length Length :: operator+ ( void ) const {return * this;}

(actually I have tried this and I get no error...)

... hence this line of thinking would explain why 2) is wrong and 3)
is right.

I am confused :)

Any help? thanks!
Zork


The const modifier says that the function does not modify *this. Any
statement within the function that might modify *this will be flagged as
an error. Returning non-const reference to *this will also be flagged as
an error because the calller would then be free to modify the object.

Number 1 returns a _copy_ of *this back, not "the actual *this" as you
stated. Making a copy of *this does not modiy it (or at least it should
not), so it does not violate the const modifier. The copy returned can of
course be modified, but that does not violate the const member function
promise.

Number 2 is returning a non-const reference to *this, which would allow a
caller to modify it. The const member function modifier disallows it,
hence the error.

Number 3 is returning a const reference to *this, so it can assume *this
cannot (without casting trickery) be modified by its caller. Hence there
is not violation of the const member function promise, and no error.

Hope this helps.

Gregg
Jul 22 '05 #2
> Number 1 returns a _copy_ of *this back, not "the actual *this" as you
stated. Making a copy of *this does not modiy it (or at least it should
not), so it does not violate the const modifier. The copy returned can of
course be modified, but that does not violate the const member function
promise.

Number 2 is returning a non-const reference to *this, which would allow a
caller to modify it. The const member function modifier disallows it,
hence the error.

Number 3 is returning a const reference to *this, so it can assume *this
cannot (without casting trickery) be modified by its caller. Hence there
is not violation of the const member function promise, and no error.

Hope this helps.


wooow.. thanks a lot!

So when a function returns an object of type reference-to-object, it returns
the actual object back, however if the function is of type object, it
returns a copy of the object.

There are so many tricks to all this, it can get slightly confusing at
times. would you have any recommendations/ books?

Thanks for the help!

Zork
Jul 22 '05 #3

"Zork" <Zo**@z.com> wrote in message
news:ec******************************@news.teranew s.com...

There are so many tricks to all this, it can get slightly confusing at
times. would you have any recommendations/ books?

You can find some great book reviews at:
http://www.accu.org/bookreviews/publ...nner_s_c__.htm

You may like to try "Accelerated C++" by Koenig & Moo. I've not read this
book but I'm getting tired of people who have read it going on and on about
it.

Another good option is Bruce Eckel's "Thinking in C++" - the big advantage
in this case is that it can be downloaded from the author's website(free of
cost!):
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

Regards,
Sumit.
--
Sumit Rajan <su********@myrealbox.com>
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Rom | last post: by
7 posts views Thread by Kelly Mandrake | last post: by
10 posts views Thread by S.Tobias | last post: by
9 posts views Thread by July | last post: by
39 posts views Thread by Martin Jørgensen | last post: by
3 posts views Thread by Beta What | last post: by
5 posts views Thread by amvoiepd | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.