|
-
January 16th, 2006, 03:37 AM
#1
overloaded function resolution...
There are 3 overloaded functions.
I would like to know why second is being called here, (on my compiler the second is being called).
Code:
#include<iostream>
using namespace std;
int func(short &a)
{
return a + 1;
}
int func(int &a)
{
return a + 2;
}
int func(long &a)
{
return a + 3;
}
int main()
{
int num = 0;
std::cout << func(num) << std::endl;
}
Thanks in advance.
Regards
Shaq
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
January 16th, 2006, 03:45 AM
#2
Re: overloaded function resolution...
You sent an int to a function and the function that ran was the one that accepted an int, correct?
Where is the confusion? Or did I miss something?
-
January 16th, 2006, 03:52 AM
#3
Re: overloaded function resolution...
 Originally Posted by kasracer
You sent an int to a function and the function that ran was the one that accepted an int, correct?
Where is the confusion? Or did I miss something?
Fine.
Lets, change the code a little, to...
(remove reference parameter, lets use copies of the parameter passed)
Code:
#include<iostream>
using std::cout;
using std::endl;
int func(short a)
{
return a + 1;
}
int func(int a)
{
return a + 2;
}
int func(long a)
{
return a + 3;
}
int main()
{
cout << func(0) << endl;
}
I hope this dosent go deep into overloaded function resolution.
Last edited by Vedam Shashank; January 16th, 2006 at 03:55 AM.
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
January 16th, 2006, 03:59 AM
#4
Re: overloaded function resolution...
 Originally Posted by Vedam Shashank
I hope this dosent go deep into overloaded function resolution.
What is the problem that you are facing? This also resolves as expected. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 16th, 2006, 04:35 AM
#5
Re: overloaded function resolution...
This resolves as expected, because 0 is an int. To call other functions either use a long integer constant (0l) or explicitly cast the integer constant ((short)0).
-
January 16th, 2006, 05:07 AM
#6
Re: overloaded function resolution...
Does that mean, if i have a constant declared within range of int..., by default, it will be an integer, and not a short or a float.
What happens if long is greater than int (in terms of size), and i declare an constant that is greater than int range. Can i assume that the function which accepts long gets called.
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
January 16th, 2006, 05:24 AM
#7
Re: overloaded function resolution...
 Originally Posted by Vedam Shashank
Does that mean, if i have a constant declared within range of int..., by default, it will be an integer, and not a short or a float.
It will not be a short. Floats always have a decimal point (e.g. 0.0, so they cannot be confused.
 Originally Posted by Vedam Shashank
What happens if long is greater than int (in terms of size), and i declare an constant that is greater than int range.
Some compilers will automatically declare this as a long constant, others will not accept it, unless you provide the long or unsigned long suffix. Probably some will give you undefined behavior by simply using a constant that fits in the range.
-
January 16th, 2006, 02:51 PM
#8
Re: overloaded function resolution...
In most cases overloading isn't beneficial. It may introduce unnecessary confusion when it's not evident which function is getting called. Though modern IDEs simplify matters somewhat.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
January 16th, 2006, 06:11 PM
#9
Re: overloaded function resolution...
The section(s) in the standard covering overload resolution are among the most complex in there. I'm still struggling to understand all the nuances.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 17th, 2006, 10:27 AM
#10
Re: overloaded function resolution...
I was asked this in an interview (a written one).
I was wondering if it would be good to have 3 such functions defined, in an actual project. I would try to avoid such, for i feel it depends so much on a programmers knowledge.
As for this...
 Originally Posted by Graham
The section(s) in the standard covering overload resolution are among the most complex in there. I'm still struggling to understand all the nuances.
That sounds like bad news to ones who wants to dig deep into overload resolution.
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
January 17th, 2006, 10:57 AM
#11
Re: overloaded function resolution...
 Originally Posted by Vedam Shashank
I was wondering if it would be good to have 3 such functions defined, in an actual project.
...Yeah, atleast I haven't come under such a situation where I would need to provide overloads with short, int and long.
By the way, you have been giving a lot of interviews lately .. all the best with those .
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|