By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Overloading by return type and a parameter value

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a
On Mon, 28 Jul 2003 22:13:56 +0200, "dropy" <dr***@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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.