[C++] Function overloading ④ (Three ways of defining function pointers | Directly define function pointers | Define function pointers through function types | Define function pointers through function pointer types)


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;
  • Function pointer assignment overloaded function: automatically match the overloaded function according to the parameter list type in the function pointer type ;




1. Function pointer definition method



Define a function first, and use different methods in this chapter to define the function pointer corresponding to the function;

// 定义一个函数
int add(int a, int b) {
    
    
	return a + b;
}

1. Directly define function pointers


According to the following function pointer syntax, define a function pointer;


"Function pointer" definition syntax:

return_type (*func_ptr)(parameter_list);
  • return_type : the return value type of the function pointed to by the function pointer;
  • func_ptr : function pointer name, the function can also be called with this name, the usage is the same as the function name;
  • parameter_list: the function pointer points to the parameter list of the function;

Directly use the function pointer definition syntax to define the function pointer;

	// 直接定义 函数指针
	int (*func3)(int a, int b) = add;

2. Define the function pointer through the function type


First, use the typedef keyword to define the function type. The following code defines the function type func. The parameter list of the function is 2 int parameters, and the return value is int;

// 定义函数类型 func , 参数列表是 2 个 int 参数 , 返回值是 int
typedef int (func)(int a, int b);

Then , define the function pointer through the defined func function type, define the function pointer directly according to the definition syntax of the pointer 指针类型* 指针名称, and assign the address of the add function to the function pointer at the same time;

	// 根据 函数类型 定义 函数指针
	func* func1 = add;

3. Define the function pointer through the function pointer type


First, through the typedef keyword, define the function pointer type, the type name is func_ptr, the parameter list of the corresponding function is 2 int parameters, and the return value is int;

// 定义函数指针类型 func_ptr , 参数列表是 2 个 int 参数 , 返回值是 int
typedef int (*func_ptr)(int a, int b);

Then, through the func_ptr function pointer type, define the function pointer, and directly use 变量类型 变量名称the method to define the function pointer;

	// 根据 函数指针类型 定义 函数指针
	func_ptr func2 = add;

4. Code example - different ways to define function pointers


In the following code, use the three methods explained in the above chapters to define function pointers;


Code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 定义函数类型 func , 参数列表是 2 个 int 参数 , 返回值是 int
typedef int (func)(int a, int b);

// 定义函数指针类型 func , 参数列表是 2 个 int 参数 , 返回值是 int
typedef int (*func_ptr)(int a, int b);

// 定义一个函数
int add(int a, int b) {
    
    
	return a + b;
}

int main()
{
    
    
	// 根据 函数类型 定义 函数指针
	func* func1 = add;

	// 根据 函数指针类型 定义 函数指针
	func_ptr func2 = add;

	// 直接定义 函数指针
	int (*func3)(int a, int b) = add;

	// 打印 调用结果 
	cout << "func1(1, 2) = " << func1(1, 2)
		<< " , func2(1, 2) = " << func2(1, 2)
		<< " , func3(1, 2) = " << func3(1, 2) << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Execution result: The function pointers defined in the three ways are all successful

func1(1, 2) = 3 , func2(1, 2) = 3 , func3(1, 2) = 3
Press any key to continue . . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132721930