[C++]C++ Pointers to functions 函数指针
来源:互联网 发布:宁波淘宝开店培训机构 编辑:程序博客网 时间:2024/05/22 02:09
C++ Pointers to functions
#include <iostream>using namespace std;void hello(){ cout << "hello ";}void world(){ cout << "world! ";}void show(void(*fun)()) { (*fun)();}int main(){ void(*pfunc2hello)() = hello; void(*pfunc2world)() = world; show(pfunc2hello); show(pfunc2world);}
Run
hello world! 请按任意键继续. . .
Note
声明函数指针
void(*pfunc2hello)() = hello;
- 声明一个函数指针,命名为
pfunc2hello
void
是被指向的函数的返回值类型,在上面的代码里是voidhello
是自己定义的一个函数的函数名
以函数指针作为参量
void show(void(*fun)()) { (*fun)();}
- 定义一个函数
show
- 接受一个函数指针(pointers to functions)作为参量(parameter),这个函数指针叫做
fun
(*fun)();
运行fun
所指向的函数
运行函数
show(pfunc2hello);
- main()函数中读取函数指针运行函数
总结
func_re_type (*def_func_ptr_name)(T a1, T a2, ...) = def_func_name
Reference
C++ Language//Compound data types//Pointers//Pointers to functions
http://www.cplusplus.com/doc/tutorial/pointers/
0 0
- [C++]C++ Pointers to functions 函数指针
- Pointers to Functions 函数指针
- 混沌 IN C++::Pointers-to-Member functions 解迷
- 指向函数的指针(pointers to functions)和回调函数
- Thinking in C++: Pointers to members 指向成员的指针
- Pointers to Functions
- Passing pointers to functions
- 6.7Pointers to Functions
- c++函数映射(Mapping Functions to a Map in C++)
- C指针学习(Pointers in C)
- [C++]C++Pointers to classes 类指针 new 与object的对比
- Pointers on C——7 Functions.1
- Pointers on C——7 Functions.2
- Pointers on C——7 Functions.3
- Pointers on C——7 Functions.4
- Pointers on C——7 Functions.5
- Pointers on C——7 Functions.6
- Pointers on C——7 Functions.7
- heap和stack区别
- JavaScript 使用误区
- Python 将字节类型转换为"十六进制"字符串
- how to find the log file of mysql
- Android+Java知识点总结-持续更新中(为了更好的自己)
- [C++]C++ Pointers to functions 函数指针
- JQuery技术
- C++中静态成员函数的常量型问题
- 自定义异常&&进程与线程
- Selenium+TestNG Web自动化测试环境搭建3_xpath学习
- Java的Random类使用
- HDU 1217 Arbitrage (floyd dp)
- HDU1276-士兵队列训练问题
- java 的23种设计模式