473,563 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2001
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
2662
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 param1, const bool = false) const;
8
2574
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. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
9
4004
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
3212
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 that const char pointer as in: void func( const char* a ) {
10
2162
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
2293
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: // based on http://msdn.microsoft.com/en-us/magazine/bb985936.aspx std::string MarshalString (const System::String^ s) {
6
3411
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 particular construct myself, and after some searching online, I found a post somewhere saying this isn't legal c++ because the standard containers...
8
4412
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 looks like this header file
28
2620
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 will be modified?
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.