Can we overload on return types

P

Panks

Hey All

Its one of basic questions? i read in a book "Thinking in C++" it says
" Overloading solely on return value is a bit
too subtle, and thus isn't allowed in C++." on page 323. While i
tried overloading on VC++ compiler i was able to overload on return
types.

Please comment


Pankaj
 
V

Victor Bazarov

Panks said:
Its one of basic questions? i read in a book "Thinking in C++" it says
" Overloading solely on return value is a bit
too subtle, and thus isn't allowed in C++." on page 323. While i
tried overloading on VC++ compiler i was able to overload on return
types.

Please give us the example of your success. Or did you mistakenly
omit the "not" between "was" and "able"?

V
 
S

sharmaharish

Pankaj,

I don't think that is allowed by C++ language or by any of its
conforming compilers. TICPP mentions this correctly. However, there is
a way by which you can get similar behaviour, by using templates. eg.

template <class T>
T overloaded_on_return_type()
{
T var;
cout << "\n" << var;
return var;
}

The type used to call the template function has to be either an
intrinsic type or it should be some UDT that has the insertion operator
overloaded for it (atleast for this example).

Calling this function is as simple as
'overloaded_on_return_type<int>();'.

Regards,
Harish Sharma
 
P

Panks

Thanks Victor i didnt miss any don'ts in my post.

Here is the code snippet and it works wen u compile with VC++ 6.0
compiler.


struct a {
int i;
};

void fun1(int i)
{
printf("\n Hello void ret");
}

float fun1(int i,int j)
{
printf("\n Hello float ret");
return 4.5;
}

a fun1(int i,double p)
{
a op;
printf("\n Hello UDD ret");
return op;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
fun1(10);
fun1(10,10);
fun1(10,10.0f);

return nRetCode;
}

Hope for QuickReply
Pankaj
 
V

Victor Bazarov

Panks said:
Thanks Victor i didnt miss any don'ts in my post.

First of all, please don't top-post. Second, please don't quote sigs.
If you don't feel the need in any of the contents of the post to which
you're replying, please take time to remove it before hitting "send".

Thanks.
Here is the code snippet and it works wen u compile with VC++ 6.0
compiler.

Oh, I have no doubt of that. Except for the missing 'include' and
the wrong definition of the 'main' function, it's normal C++ and should
compile anywhere.
struct a {
int i;
};

void fun1(int i)
{
printf("\n Hello void ret");
}

float fun1(int i,int j)
{
printf("\n Hello float ret");
return 4.5;
}

a fun1(int i,double p)
{
a op;
printf("\n Hello UDD ret");
return op;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
fun1(10);
fun1(10,10);
fun1(10,10.0f);

So, could you explain to me why do you think that you're overloading
*on* the return value type rather than on the number and the types of
the arguments here? What *role* does the return value type play in
the compiler's selecting the right function to call?
return nRetCode;
}

Hope for QuickReply
Pankaj

V
 
N

Noah Roberts

Panks said:
Hey All

Its one of basic questions? i read in a book "Thinking in C++" it says
" Overloading solely on return value is a bit
too subtle, and thus isn't allowed in C++." on page 323. While i
tried overloading on VC++ compiler i was able to overload on return
types.

It is only possible when you are overriding a function in a base class
with a return value that derives from the return of the target
function:

struct A {};
struct B : A {};

struct C
{
A * whatever();
};

struct D : C
{
B * whatever();
};

More commonly you use this to implement a polymorphic copy function.

I know of no other condition that allows this...I am not sure if being
a pointer or reference is a requirement though it wouldn't surprise me
if it was.
 
P

Panks

Hi

I thought to remind you of post anyways. There is no foul play in
original post a gave you strict statement from a books that overloading
on return type is not supported.
Ok if now if i am able to compile the code and run it hence compiler
accepts this overloaded definition. No matter i have overloaded it on
parameters also. You can add code to accept the return values it runs
even that also error free.

Point is why is the contradiction? Rather looking for header file
stement i hope you focus on core issue.

Pankaj
 
V

Victor Bazarov

Panks said:
I thought to remind you of post anyways. There is no foul play in
original post a gave you strict statement from a books that
overloading on return type is not supported.
Ok if now if i am able to compile the code and run it hence compiler
accepts this overloaded definition. No matter i have overloaded it on
parameters also. You can add code to accept the return values it runs
even that also error free.

Point is why is the contradiction? Rather looking for header file
stement i hope you focus on core issue.

I can barely understand what your point is. Let me explain what is
meant by "overloading on return value". If we remove all other function
signature differences (name, number and types of arguments), all that
remains is the return value type. Now, given that two functions *only*
differ in the return value type, is it allowed? If it were allowed, the
following would be legal code:

int foo();
double foo();

int main() {
return foo();
}

and the function 'foo' chosen in the 'main' function would be the one
that retuns an int. Now, since it _isn't_ allowed, the compiler will
refuse to compile this code.

In your example functions had different number and types of arguments.
And that's how the compiler knows which function to use. Return value
types play _no_role_ there. You can make them all the same, you can
make them different, it *does not matter*.

V
 
V

Victor Bazarov

Noah said:
It is only possible when you are overriding a function in a base class
with a return value that derives from the return of the target
function:

struct A {};
struct B : A {};

struct C
{
A * whatever();
};

struct D : C
{
B * whatever();
};

More commonly you use this to implement a polymorphic copy function.

I know of no other condition that allows this...I am not sure if being
a pointer or reference is a requirement though it wouldn't surprise me
if it was.

I am not sure why you gave this example. There is no *overloading* in
your code. None. Whatsoever.

The only time where return value types begin playing some role in the
function signature (and therefore in picking which function to use) is
in pointer-to-function conversions from a template. Observe:

template<class T,class U> T foo(U) { return T(); }

int main() {
double (*p)(int) = foo;
}

Here, the template argument 'T' is deduced from the function type, and
that's the only place where it matters.

V
 
P

Panks

Thanks Harish

but pls below code and it executes wen compiled by VC++ compiler

struct a {
int i;
};

void fun1(int i)
{
printf("\n Hello void ret");
}

float fun1(int i,int j)
{
printf("\n Hello float ret");
return 4.5;
}

a fun1(int i,double p)
{
a op;
printf("\n Hello UDD ret");
return op;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
fun1(10);
float f=fun1(10,10);
a vara=fun1(10,10.0f);
return nRetCode;
}


Its overloading right? Here along with parameter overloading is done
also on return types. Isnt this the contradiction of statement TICPP.
Although i accept overloading of type :
void fun();
int fun();

is not allowed. Please comment

Pankaj
 
P

Panks

Thanks Harish

but pls below code and it executes wen compiled by VC++ compiler

struct a {
int i;
};

void fun1(int i)
{
printf("\n Hello void ret");
}

float fun1(int i,int j)
{
printf("\n Hello float ret");
return 4.5;
}

a fun1(int i,double p)
{
a op;
printf("\n Hello UDD ret");
return op;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
fun1(10);
float f=fun1(10,10);
a vara=fun1(10,10.0f);
return nRetCode;
}


Its overloading right? Here along with parameter overloading is done
also on return types. Isnt this the contradiction of statement TICPP.
Although i accept overloading of type :
void fun();
int fun();

is not allowed. Please comment

Pankaj
 
V

Victor Bazarov

Panks said:
Thanks Harish

but pls below code and it executes wen compiled by VC++ compiler

struct a {
int i;
};

void fun1(int i)
{
printf("\n Hello void ret");
}

float fun1(int i,int j)
{
printf("\n Hello float ret");
return 4.5;
}

a fun1(int i,double p)
{
a op;
printf("\n Hello UDD ret");
return op;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

This is not C++. Please when posting to comp.lang.c++ try to
remove any compiler-specific elements. Example: _tmain is MS,
and so is TCHAR. We here prefer this definition of 'main':

int main()

(no need for arguments which you aren't using).
{
int nRetCode = 0;
fun1(10);
float f=fun1(10,10);
a vara=fun1(10,10.0f);
return nRetCode;
}


Its overloading right?
Yes.

Here along with parameter overloading is done
also on return types.

No. Return value types have *nothing* to do with the overloading
that you have happening there.
Isnt this the contradiction of statement TICPP.
Although i accept overloading of type :
void fun();
int fun();

is not allowed. Please comment

Done. And please don't post twice.

V
 
S

shadowman615

Panks said:
Its overloading right? Here along with parameter overloading is done
also on return types. Isnt this the contradiction of statement TICPP.
Although i accept overloading of type :
void fun();
int fun();

is not allowed. Please comment

Pankaj

No,. read the statement you quoted again (emphasis mine) :
"Overloading ***SOLELY*** on return value is a bit too subtle, and thus
isn't allowed in C++."
 
P

Panks

Victor Bazarov

Had enough of you. Pls dont reply to my post as i put up question for
Harish Sharma. Oversmartness is harmfull too health.

No need too reply.

Pankaj
 
P

Panks

Ok shadowman

Now you make things clear.

"Overloading ***SOLELY*** on return value is a bit too subtle, and thus
isn't allowed in C++."

Victor
I m sorry as i sounded harsh but thanks for ur help too

Thanks a lot
Pankaj
 
V

Victor Bazarov

Noah said:
Whatever, Vic.

Please don't address me "Vic" until we drink ourselves to a stupor together.
If you need an explanation of the difference between overloading,
overriding,
and hiding, and what happens in your example, just ask.
 
F

Frederick Gotham

Noah Roberts posted:

Whatever, VIC.


Victor is correct. You're incorrect. To compound the matter, you've
decended into imbecilic name-calling. How mature.

Perhaps take Victor up on his kind offer of explaining overloading,
overriding, and hiding; you'll learn from it -- plus you'll be
demonstrating to the group that you can admit when you're wrong, and learn
from the experience.

If you'd like to address me derogatorily as a response to this post, may I
suggest one of:

Freddie
Poo face
Fred
Stupey head

Forgive me, it's been a few decades since I've called people names so I
don't come out with the best material.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top