const vs non const functions signature

A

arun

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
 
V

Victor Bazarov

arun said:
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
 
I

Ian Collins

arun said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top