使用函数指针
bool lengthcompare(const string&, const string &); pf = lengthcompare pf = &lengthcompare//两种形式都可以 bool (*pf)(const string &, const string &);//标准函数指针声明格式 bool b1 = pf("hello", "goodbye"); bool b2 = (*pf)("hello", "goodbye");//两种等价的函数调用
可以用函数指针作为函数形参,因为函数不能用来做形参,这是可以直接把函数名用来做实参,编译器会自动转化成指针
返回指向函数的指针
//利用类型别名进行讨论 typedef bool Func(const string&); typedef decltype(lengthcompare) Func2;//两个都是函数类型 typedef bool (*Funcp) (const string&); typedef decltype(lengthcompare) *Funcp2;//两个都是函数指针类型 using F = int(int*, int); using PF = int(*)(int*, int); PF f1(int);//返回函数指针的函数f1 F *f1(int);//返回函数指针的函数f1(显式的指出) int (*f1(int)) (int*, int); auto f1(int) -> int(*)(int* ,int);