Article Directory
Blog summary:
- Overloaded functions: use the same function name to define different function parameter lists;
- Judgment criteria: Only the difference in the number/type/order of function parameters is the criterion for "function overloading", and the return value of the function is not the criterion for "function overloading";
- Ambiguity: If function overloading is used in combination with default parameters, there will be ambiguity, and the compilation will fail directly;
1. Function overloading
1. Analysis of overloaded function calls
Overloaded function call query analysis: Call an overloaded function, how to find out the function you want to call from multiple overloaded functions, the process is as follows:
First, select the function with the same name, and select the function that matches the function name of the calling function according to the function name as a candidate function;
Then, try to find the target function from the candidate functions, exactly matching the parameter list of the passed-in actual participation in the overloaded function;
- The first step is to check whether the actual participation formal parameters can be accurately matched through the default parameters . If the matching fails, continue to the second step;
- The second step is to convert the default parameter type to see if the match is successful;
- If the above 2 steps fail to match, or if multiple matching functions are found, the compilation will fail;
2. Features of function overloading
Function overloading features:
- Independent of each other: overloaded functions are essentially different functions, and the overloaded functions are independent of each other without any connection;
- Different types: The function types of the overloaded functions are different;
- Unique identification: overloaded functions can only be uniquely identified through the function name and parameter list;
- Do not judge the return value: only the difference in the number/type/order of function parameters is the judgment standard of "function overloading", and the return value of the function is not the judgment standard of "function overloading";
2. Function overloading and default parameters
1. Ambiguity analysis of function overloading and default parameters
Define 2 functions, one of which uses default parameters and the other does not use default parameters. These two functions have the same function name and are overloaded functions;
Define the first function, receive 3 integer parameters, and the last parameter is the default parameter;
Note: When the function 1 is called, it can be called with fun(1, 2) or fun(1, 2, 3);
// 函数 1 : 接收 3 个整数参数 , 最后一个参数是默认参数
void fun(int i, int j, int k = 10) {
cout << "i = " << i << "j = " << j << "k = " << k << endl;
}
Define the second function, which receives 2 integer parameters and can only be called in the form of fun(1, 2);
// 函数 2 : 接收 2 个整数参数
void fun(int i, int j) {
cout << "i = " << i << "j = " << j << endl;
}
2. Code example - define the above two functions
If you just define overloaded functions and default parameter functions without calling them, the compilation will not report an error;
Code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 函数重载 + 默认参数
// 函数 1 : 接收 3 个整数参数 , 最后一个参数是默认参数
void fun(int i, int j, int k = 10) {
cout << "i = " << i << "j = " << j << "k = " << k << endl;
}
// 函数 2 : 接收 2 个整数参数
void fun(int i, int j) {
cout << "i = " << i << "j = " << j << endl;
}
int main()
{
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Execution result: If you just define functions without calling them, the compilation will not report an error;
3. Code example - function call without ambiguity
If you just call fun(1, 2, 3), it can also be executed successfully;
Because function 1 can be uniquely located void fun(int i, int j, int k = 10)
, there will be no ambiguity;
// 函数 1 : 接收 3 个整数参数 , 最后一个参数是默认参数
void fun(int i, int j, int k = 10) {
cout << "i = " << i << " , j = " << j << " , k = " << k << endl;
}
Code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 函数重载 + 默认参数
// 函数 1 : 接收 3 个整数参数 , 最后一个参数是默认参数
void fun(int i, int j, int k = 10) {
cout << "i = " << i << " , j = " << j << " , k = " << k << endl;
}
// 函数 2 : 接收 2 个整数参数
void fun(int i, int j) {
cout << "i = " << i << "j = " << j << endl;
}
int main()
{
fun(1, 2, 3);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
i = 1 , j = 2 , k = 3
Press any key to continue . . .
4. Code example - ambiguous compilation failure
If fun(1, 2) is executed, it will match function 1 and function 2 at the same time. At this time, there is ambiguity, and an error will be reported when compiling;
Code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 函数重载 + 默认参数
// 函数 1 : 接收 3 个整数参数 , 最后一个参数是默认参数
void fun(int i, int j, int k = 10) {
cout << "i = " << i << " , j = " << j << " , k = " << k << endl;
}
// 函数 2 : 接收 2 个整数参数
void fun(int i, int j) {
cout << "i = " << i << "j = " << j << endl;
}
int main()
{
fun(1, 2);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(22,10): error C2668: “fun”: 对重载函数的调用不明确
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(16,6): message : 可能是“void fun(int,int)”
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(11,6): message : 或 “void fun(int,int,int)”
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(22,10): message : 尝试匹配参数列表“(int, int)”时
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========