zoukankan      html  css  js  c++  java
  • Java程序猿学习C++之函数指针

    函数指针的定义:



    头文件: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();
    }
    


  • 相关阅读:
    Python判断操作系统类型
    Linux之计划任务
    java web 之 WebRoot和WebContent目录
    天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)
    MyEclipse开发的java web项目在 Eclipse中无法识别
    Tomcat 改BUG之 localhost:8080 404
    (重要) html概念之 input:name与id详解
    html基础之 input:type
    BeanUtils 以及BeanUtils.populate使用
    bootstrap 之 列表组件使用
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6950502.html
Copyright © 2011-2022 走看看