Overloading by return type and a parameter value



 DEVELOP > c-Plus-Plus > Overloading by return type and a parameter value

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "dropy"
Date: 28 Jul 2003 03:13:56 PM
Object: Overloading by return type and a parameter value
Hi, this is a oversimplification of my problem:
I have two functions:
double foo(int param);
int foo(int param);
I want 'double function' to be called if param=1
and 'int function' if param=2
foo(1) => double foo(1)
foo(2)=> int foo(2)
Is this possible?
Thanks
.

User: "Jonathan Mcdougall"

Title: Re: Overloading by return type and a parameter value 28 Jul 2003 03:59:02 PM
On Mon, 28 Jul 2003 22:13:56 +0200, "dropy" <dropy@dododo.com> wrote:

Hi, this is a oversimplification of my problem:

I have two functions:

double foo(int param);
int foo(int param);

I want 'double function' to be called if param=1
and 'int function' if param=2

foo(1) => double foo(1)
foo(2)=> int foo(2)

Is this possible?

A simple way would be
double d_foo();
int i_foo();
int foo(int param)
{
if (param == 1)
d_foo();
else if (param == 2)
i_foo();
}
Another way would be
template <int param> class foo;
template <>
class foo<1>
{
public:
double operator()( /* something */ )
{
// do something
}
};
template <>
class foo<2>
{
public:
int operator()( /* seomthing */ )
{
// do something
}
};
int main()
{
foo<1> f;
f(); // calls double operator()
foo<2> g;
g(); // calls int operator()
}
Jonathan
.

User: "Artie Gold"

Title: Re: Overloading by return type and a parameter value 28 Jul 2003 03:58:24 PM
dropy wrote:

Hi, this is a oversimplification of my problem:

I have two functions:

double foo(int param);
int foo(int param);

I want 'double function' to be called if param=1
and 'int function' if param=2

foo(1) => double foo(1)
foo(2)=> int foo(2)

Is this possible?

Nope. You can neither overload by argument _value_ nor by return value.
HTH,
--ag
--
Artie Gold -- Austin, Texas
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER