How function overload resoultion takes place in C++?
I am a beginner in C++. I wanted to know, how internal开发者_如何学JAVAly the function overloading takes place in C++
I recommend you reading C++ Primer. This concept is explained in depth.
On a higher level, function overloading is allowed if both the functions
- have same name
- declared in the same scope
- have different parameter list.
On the lower-level ( inside how compiler figures it out), here's how it is done.
There are 3 steps for function overloading resolution.
e.g void f();
void f(int);
void f(double, double = 3.4);
void f(char *, char *);
Function call inside main -> void f(5.6);
Identifies the set of overloaded functions considered for the call, they are called as candidate functions. A candidate function is a function with the same name as the function that is called & for which declaration is visible at the point of call. It also identifies the properties of the argument list in the function call, i.e no of arguments and their types.
Selected : All 4 functions with name 'f'
Selects the function from the set of candidate functions found in step 1 that can be called with arguments specified in the call. Those are called as viable functions. A viable function is a function that has the same nof of parameters or more parameters ( addn paramters has an associated default argument) than the arguments in the actual function call. Types of arguments must be convertible for the function to be classified as viable.
Selected : void f(int) and void (double, double =3.4)
Identifies the best viable function among the all viable functions. For (int) conversion need to apply which is a standard conversion (double to int). But for (double, double=3.4), there's a exact match, so no conversion needed. No conversion is better than a conversion.
Selected : void (double, double = 3.4 )
It takes place internally with mangled names.
For the file
void blah(int f)
{
}
void blah(double f)
{}
I get the symbols
ethan@EthanPc ~ % nm blah.o
0000000000000009 T _Z4blahd
0000000000000000 T _Z4blahi
Notice the d for double, and the i for integer.
精彩评论