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

const vs non const functions signature

Hello Team,

I have a question on const vs non const and object return vs non object
return function and
which one will be called

ie.

void foo(); vs
void foo() const;

which one willl be called and under what circumstances. C++ allows
both signatures inside
the same class,

similarly

T bar();
T& bar();

which one will be called and under what circumstances.

and also a feedback on the following signatures.
int foo2(); vs
const int foo2();

Kindly provide an answer. It will be highly appreciated.

Thanks.

arun

May 1 '06 #1
5 1980
arun wrote:
I have a question on const vs non const and object return vs non
object return function and
which one will be called

ie.

void foo(); vs
void foo() const;

which one willl be called and under what circumstances.
For objects that are 'const', the 'const' version is called. For
non-const objects the non-const version is called.
C++ allows
both signatures inside
the same class,

similarly

T bar();
T& bar();
That is not allowed. You're not allowed to overload based on return
value type alone.
which one will be called and under what circumstances.
Neither. The code containing those declarations will not compile.
and also a feedback on the following signatures.
int foo2(); vs
const int foo2();


What feedback are you expecting? The latter while legal makes no
semantic sense. Both functions 'foo2' return an rvalue, which if
of type 'int' cannot be cv-qualified. And, again, if both those
declarations exist in the same scope, the program is ill-formed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '06 #2
arun wrote:
Hello Team,

I have a question on const vs non const and object return vs non object
return function and
which one will be called

You asked this a couple of days ago, why ask again?

--
Ian Collins.
May 1 '06 #3
Sorry, Thought it did not reach the audience. Thanks for your reply.

arun

May 2 '06 #4
Sorry, Thought it did not reach the audience. Thanks for your reply.

arun

May 2 '06 #5
Sorry, Thought it did not reach the audience. Thanks for your reply.

arun

May 2 '06 #6

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

Similar topics

14
by: mandydhaliwal | last post by:
Hi Guys, I was going through a product's source code. They never use const function arguments with default value. for instance Never noticed following type of declaration foo ( const int...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
9
by: red floyd | last post by:
Anybody have any preferences as to declaring primitive type parameter as const, if the function body doesn't change it? e.g.: int f(int x) { return x+2; }
3
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to...
10
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
2
by: Kenneth Porter | last post by:
I try to be const correct everywhere and found that I can't pass a const String to this API. Is it going to change the value? Or is the signature not the best it could be? I want to write this:...
6
by: muzicmakr | last post by:
I'm porting some code from windows to mac, and there are some instances of std::vector<const MyType>, that compiled just fine on the pc, but won't compile under gcc. I'd never tried to do this...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
28
by: lovecreatesbeauty | last post by:
Is the keyword const necessary in the comparison function in qsort and bsearch? int (*compar)(const void *, const void *) If the pointer cannot be dereferenced why worry if the pointed object...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.