473,404 Members | 2,137 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,404 software developers and data experts.

Fun with const

Consider:

const bool func() {}

and

bool func() const {}

Are they different?
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 28 '06 #1
6 2205
Yes

First one is function returning constant bool and the second one is
constant member function.

May 28 '06 #2
Alex Buell posted:

const bool func() {}

This may or may not be a member function -- the code snippet is too small
to tell.

The function takes no arguments.

The function returns a const bool by value.
bool func() const {}

This is a const member function.

The function takes no arguments.

The function returns a bool by value.

Are they different?

I presume you're alluding to the difference between:

int SomeFunc();

int const SomeFunc();
To my knowledge, there's no discernable difference if you're dealing with
a primitive type -- however there certainly is a difference when dealing
with a fancy class type:
int main()
{
SomeFunc().SomeNonConstMemberFunction();

/* Above line won't work if const object
is returned by value. */
}

-Tomás
May 28 '06 #3
Tomás wrote:
I presume you're alluding to the difference between:

int SomeFunc();

int const SomeFunc();

To my knowledge, there's no discernable difference if you're dealing with
a primitive type


In the latter case, the return value cannot be used as an lvalue.
There's generally no reason to *want* to use it as an lvalue, so it's
often good to make return-by-value types const to prevent accidents.

One other point -- these two functions do not have different
signatures.

Luke

May 28 '06 #4
Luke Meyers wrote:
Tomás wrote:
I presume you're alluding to the difference between:

int SomeFunc();

int const SomeFunc();

To my knowledge, there's no discernable difference if you're dealing with
a primitive type


In the latter case, the return value cannot be used as an lvalue.
There's generally no reason to *want* to use it as an lvalue,


std::auto_ptr<MyClass> createObject();

May 28 '06 #5
Luke Meyers posted:
Tomás wrote:
I presume you're alluding to the difference between:

int SomeFunc();

int const SomeFunc();

To my knowledge, there's no discernable difference if you're dealing
with a primitive type


In the latter case, the return value cannot be used as an lvalue.
There's generally no reason to *want* to use it as an lvalue, so it's
often good to make return-by-value types const to prevent accidents.

You worded that paragraph as if the opposite was true for the version
whose return value isn't const.

Neither of them can be used as an L-value:

int ReturnModifiableByValue();
int const ReturnConstByValue();

int main()
{
ReturnModifiableByValue() = 7; /* ERROR */

ReturnConstByValue() = 3;
}
-Tomás
May 28 '06 #6
Tomás wrote:
Luke Meyers posted:
In the latter case, the return value cannot be used as an lvalue.
There's generally no reason to *want* to use it as an lvalue, so it's
often good to make return-by-value types const to prevent accidents.


You worded that paragraph as if the opposite was true for the version
whose return value isn't const.

Neither of them can be used as an L-value:


This falls under the category of "could swear I heard that somewhere,
can't remember the cite, and should have confirmed it before posting."
Thanks.

Luke

May 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.