Java程序员学习C++之函数指针
来源:互联网 发布:网络机顶盒品牌知乎 编辑:程序博客网 时间:2024/06/05 05:57
函数指针的定义:
头文件:function.h
//该头文件定义了接口,在实际的源文件中定义了模板方法//后续的调用类只需要实现覆盖before,process,after方法即可实现模板方法的回调/*extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量或函数时,在其它模块中寻找其定义。*/extern void(*before)();extern void(*process)(int,int);extern void(*after)();void my_function(int a,int b);
源文件main.cpp
#include "function.h"#include <iostream>using namespace std;extern void dosth();//static标记该方法的作用域为该模块(文件)内static void before_pro(){cout << "main.before_pro" << endl;}static void process_pro(int a, int b){cout << "main.process_pro" << endl;}static void after_pro(){cout << "main.after_pro" << endl;}//void my_function(int a, int b)//{//before();//process(a, b);//after();//}int main(){before = before_pro;process = process_pro;after = after_pro;my_function(2,3);dosth();return 0;}
源文件:test.cpp
#include <iostream>#include "function.h"using namespace std;void(*before)();void(*process)(int, int);void(*after)();static void before_pro(){cout << "test.before_pro" << endl;}static void process_pro(int a, int b){cout << "test.process_pro" << endl;}static void after_pro(){cout << "test.after_pro" << endl;}void dosth(){before = before_pro;process = process_pro;after = after_pro;my_function(2, 3);}void my_function(int a, int b){before();process(a, b);after();}
0 0
- Java程序员学习C++之函数指针
- 黑马程序员----C 语言学习笔记之指针函数与函数指针
- C学习之------C函数指针
- c语言学习笔记之函数指针和指针函数
- JAVA程序员学习C++之指针
- Java程序员学习C++之常量指针和指针常量
- 黑马程序员-----C语言学习之函数
- C函数指针学习
- C-[之函数指针]
- 【黑马程序员】C语言学习笔记之指针(十一)
- 黑马程序员-C语言学习之指针总结
- 黑马程序员-c语言学习之指针的心得
- 黑马程序员IOS基础---C语言学习之指针
- 黑马程序员------c语言学习笔记之指针
- 黑马程序员-----C语言学习之数组与指针
- 黑马程序员---C 语言学习笔记之指针
- Java程序员学习C++之函数
- 《C和指针学习笔记》之sscanf函数的用法
- springMVC 4.1 环境搭建
- linux的sort命令
- java实现整个工程全局变量的方法
- 黑马程序员-java集合
- vi 阅读代码必备命令
- Java程序员学习C++之函数指针
- RADIUS
- 如何使用SSH连接OpenStack上的云主机
- MobileWeb适配(一)
- 科研心得体会
- AWWWB.COM网站克隆器
- Keil 界面配置保存和共享
- android studio使用遇到问题
- C# 加密解密(DES,3DES,MD5,Base64) 类