C++ — Basic of function overload
What is function overload
Function overload refers to overwriting without any commonality other than the name of the function
This concept can be confused with function override, which refers to the derivation class redefining the virtual function of the underlying class
Overload Resolution
If it turns out to be one of the overloaded functions when the function is called, the following procedure must be used to determine which of all overload functions is most appropriate: This is called overload resolution
- An exact match was found.
- A trivial conversion was performed.
- An integral promotion was performed.
- A standard conversion to the desired argument type exists.
- A user-defined conversion (either a conversion operator or a constructor) to the desired argument type exists.
- Arguments represented by an ellipsis were found.
If more than one match is found at the highest level where the match is found, the call is judged to be ambiguous and is not compiled
If you would like to learn more about trivial conversion and integral promotion, please refer to the following link:
A brief description of each of the trivial conversion and integral promotion is as follows:
trivial conversion — with array name as pointer, function name as pointer to function, and T as constT
integral promotion — from bol to int, char to int, short to int, signed integer to unsigned integer
Overload and Return Type
The return type of the function is not considered in the overload resolution
Overload and Valid Scope
Overload occurs between members of the overload set
This means that a function of a single effective range, i.e., a function declared in a non-namespace effective range, is not overloaded
void f(int)
{
std::cout << "f(int)" << std::endl;
}
namespace Sample
{
void f(double)
{
std::cout << "f(double)" << std::endl;
}
void Test()
{
f(1); // calls f(double)
}
}
In other words, the concept of function overload does not apply in the case of the above example