博客
关于我
函数指针
阅读量:314 次
发布时间: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/

你可能感兴趣的文章
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>