Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 04:49 PM
dropy
Guest
 
Posts: n/a
Default 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


  #2  
Old July 19th, 2005, 04:49 PM
Artie Gold
Guest
 
Posts: n/a
Default Re: Overloading by return type and a parameter value

dropy wrote:[color=blue]
> 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?[/color]

Nope. You can neither overload by argument _value_ nor by return value.

HTH,
--ag



--
Artie Gold -- Austin, Texas

  #3  
Old July 19th, 2005, 04:49 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default Re: Overloading by return type and a parameter value

On Mon, 28 Jul 2003 22:13:56 +0200, "dropy" <dropy@dododo.com> wrote:
[color=blue]
>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?[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles