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