confusion with const functions return types

Z

Zork

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
 
G

Gregg

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
 
Z

Zork

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
 
S

Sumit Rajan

Zork said:
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/public/reviews/0sb/beginner_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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top