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
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?
Re: overloaded function resolution...
Quote:
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.
Re: overloaded function resolution...
Quote:
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.
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).
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.
Re: overloaded function resolution...
Quote:
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.
Quote:
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.
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.
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.
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...
Quote:
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.
Re: overloaded function resolution...
Quote:
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 :thumb: .