473,385 Members | 1,645 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,385 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 1578
"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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.