473,219 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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 1571
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zork | last post by:
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...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
8
by: Rom | last post by:
I'm a bit confused as to how the STL set works with the <setname>.insert() and <setname>.find() functions Compiler used : CodeWarrior 5.0 My intial interpretation was that I needed to overload...
7
by: Kelly Mandrake | last post by:
I've implemented a class with operator+ overloaded and I also provided my own copy constructor; The problem is my copy constructor is being called twise and I dont understand why. I believe the...
10
by: S.Tobias | last post by:
1. If I have a struct type which contains a const member: struct mystruct { const int id; int mutable; }; does a definition struct mystruct ms; define an object `ms' which is *partly* const? ...
9
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.