Pointers to Function
来源:互联网 发布:郑州软件开发 编辑:程序博客网 时间:2024/05/21 21:34
Function type
A function type is determined by its return value and parameter list
Pointers to function
a pointer to funtion is a pointer points to a object of particular function type
bool (*pf) (const string&, const string&);//definition
The parentheses is necessary
bool *pf(const string&, const string&);//declares a function named pf that returns a bool*
using typedefs to simplify function pointer definition
typedef bool (*cmp)(const string&,const string&);cmp pf1=0;//initialize a pointer with null
Initialization and Assignment
function name is implicit pointer of that function type
cmp pf1=lengthCompare;//equivalent to cmp pf1=&lengthCompare(why?)
There is no conversion between one pointer to function type and another
Application of function pointer
1 calling a function through a function pointer
bool lengthCompare(const string& s1,const string& s2){return s1.size()==s2.size()? true : false ;}
typedef bool (*cmp)(const string&,const string&);cmp pf1=lengthCompare;//initialize a pointer with nullstring s1="abc";string s2="abcsfd";bool flag;flag=lengthCompare(s1,s2);//method 1flag=pf1(s1,s2);//equivalent to *pf1(s1,s2)
2 passing a function as parameter to another
bool isSameLength(const string& s1,const string& s2, bool(*f)(const string& ,const string&)){ if(f(s1,s2)) return true; else return false;}
just a silly function
3 returning a pointer to function
example, a function returns a pointer to function type of int(int*,int)
int (*f(int))(int*,int);
for simplicity still typedef can be used
typedef int(*PF)(int*,int)//syntax for typedef int(*)(int*,int) PF
PF f(int)
the return type of a function can only be pointer to function,but not function type itself(why?)
typedef int func(int,int)//func is a function type,not a pointer to functionvoid f1(func);//the parameter convert automatically to func*func f2(int); //errorfunc *f3(int);//ok
- Pointers to Function, ++
- Pointers to Function
- Function Pointers
- Function Pointers
- Function pointers
- How to use (data_member | member function) pointers in class
- Pointers to Pointers
- Abstraction and function pointers
- Chapter 22: Pointers to Pointers
- SingleLinkNode with Pointers-to-Pointers
- 函数指针(Function Pointers)
- Programs as Data: Function Pointers
- Example Uses of Function Pointers
- Function Pointers, Functors, Virtual Functions
- Pointers to Arrays
- A Prelude to Pointers
- Pointers to Functions
- Passing pointers to functions
- VB.NET版机房收费系统之组合查询
- 关于wordpress密码的破解思路
- 黑马程序员-Map集合
- EF中提示一个或者多个验证出现错误,查看 EntityValidationErrors 详细信息的解决方法
- 越狱开发教程-第一个Tweak
- Pointers to Function
- 国产手机OS,谁能定制时代?(我最看好阿里云os 其次就百度云os了,期待阿里云os赶快普及啊!)
- IOS音乐后台播放的实现
- Winform 部署mshtml程序集出错的一个解决方案 .
- Hdu 4501 小明系列故事——买年货
- YUV420、YUV422、RGB24转换
- 数学题目汇总
- UVALive 4004 Space Beacon
- Ubuntu系统上轻松截图的几种方法介绍