博客
关于我
函数指针
阅读量:301 次
发布时间:2019-03-04

本文共 1325 字,大约阅读时间需要 4 分钟。

1、解释

(1)函数指针指向的是函数而非对象;和其他指针一样,函数指针指向某种特定类型;

(2)函数的类型由它的返回类型和形参类型决定,与函数名无关;

2、声明

bool lengthCompare(const string &, const string &)

(1) bool (*pf) (const string &, const string &);

(2) using F=int(int* , int);

(3) typedef decltype(lengthCompare) *FuncP2

(4) typedef bool Func(const string&, const string&)

3、初始化

pf = lengthCompare;

pf=&lengthCompare;

pf = 0;

4、调用:形参和返回类型必须完全匹配

bool b1 = pf("hello", "goodbye");

bool b2 = (*pf)("hello", "goodbye");

bool b3 = lengthCompare("hello", "goodbye");

5、重载函数的指针:编译器通过指针类型决定选用哪个函数;精确匹配;

void ff(int *);

void ff(unsigned int);

void (*fn)(unsigned int) = ff;

6、函数指针形参:不能定义函数类型的形参,跟数组一样,会自动转换

void useBigger(const string &s1, const string &s2, bool pf(const string&, const string&)); //自动转换为函数指针

void useBigger(const string &s1, const string &s2, bool (*pf)(const string&, const string&));

7、类型别名

(1) typedef bool Func(const string&, const string&);//函数类型

(2) typedef decltype(lengthCompare) Func2;//函数类型

(3) typedef bool(*FuncP)(const string&, const string&); //函数指针

(4) typedef decltype(lengthCompare) *FuncP2;// decltype的结果是函数类型

void useBigger(const string &s1, const string &s2, Func2);

8、返回函数指针

(1) using F= int(int*, int);

F  *f1(int) ;

(2) using PF=int(*)(int* ,int)

PF f1(int) ;

(3) int (*f1 (int))( int*, int);

(4) 尾置返回类型的方式

auto f1(int) -> int(*)(int*, int);

(5)decltype简化函数返回类型

 

转载地址:http://hkpq.baihongyu.com/

你可能感兴趣的文章
[CF1278F]Cards
查看>>
jQuery实现无刷新切换主题皮肤功能
查看>>
[CF932E]Team Work
查看>>
用postman测试url参数
查看>>
Random IS
查看>>
Vue的is属性
查看>>
Vue爬坑之v-model和v-bind(二)
查看>>
神犇和蒟蒻
查看>>
vue组件传参 props default 数组/对象的默认值应当由一个工厂函数返回
查看>>
vue爬坑之 父组件向子组件异步传参 子组件中拿不到值的解决方法
查看>>
js基础复习5-原型链与js的成员查找机制
查看>>
js基础复习8-call方法简单使用以及javascript继承
查看>>
【游记】被吊打DAY2
查看>>
6.docker迁移与备份
查看>>
ThinkCMF报错未定义变量vo
查看>>
微信公众号开发之素材管理
查看>>
mysql limit 1 (报错信息:Subquery returns more than 1 row)
查看>>
修改dynamic web module的版本大小
查看>>
IDEA 成功在tomcat上部署项目
查看>>
Node.js response 页面中文乱码
查看>>