函数类型(FuncType)和函数指针(FuncPointer)
函数指针: 指向函数的的指针,和普通的指针无本质区别
函数类型: 由函数返回值类型/函数参数类型决定,和函数名称无关
例如:
对于函数: bool my_function(int a, const std::string& str)
函数类型(FuncType): bool(int, const std::string&)
函数指针FuncPointer: bool (*FuncPointer)(int, const std::string&)
函数类型和函数指针和函数参数
(1) 函数形参: 函数类型不可作为形参(但是一般编译器会将函数类型形参自动转为函数指针),函数指针可以作为形参
例如:
void my_function2(FuncPointer fp) // OK
void my_function3(FuncType fn) ==编译器转为==> void my_function3(FuncPointer fp)
(2) 函数作为实参时,自动转为函数指针
如下示例:
#include <iostream>
typedef void(*FuncPointer)(const std::string& str);
typedef void(FuncType)(const std::string& str);
void func(const std::string& str)
{
std::cout << __FUNCTION__ << "==> " << str <<