473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Jul 19 '05 #1
2 3455
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
1
by: NR | last post by:
Hi Folks.. Below is cut paste of one of the "class" in my project... which is used in a composition relationship in many other classes. ----------------------------------------------------...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
6
by: Aire | last post by:
To implement the '+' overloading, the type of return value could be either "Class1" or "Class1&". Class1& Class1::operator+(const Class1& a, const Class1& b){ } Class1 Class1::operator+(const...
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
18
by: Daniel Gustafsson | last post by:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading. It seems like it's not possible to override method using return types, only parameters. Is that by design,...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
7
by: Marcos Stefanakopolus | last post by:
So this is ok: class foo { int myMethod(string, int) { ... } int myMethod(string, double) { ... } } But this is not: class foo {
14
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.