• 函数指针


    前言:

    先看两个基础,函数指针和extern关键字,然后由一个具体的例子,具体使用下函数指针。

    一、基础

    函数指针:即指向函数的指针,本质还是一个指针。
    函数指针的声明:返回值类型 ( * 指针变量名) ([形参列表]);
    注意这里是声明不是定义,声明之后它就是一个类型了(与int,char,int *等级别等同,这点有点像结构体),然后就可以定义、使用了。
    举例如下(下面这段小程序摘自百度百科):

    int max(int x,int y){return (x>y? x:y);}
    int main()
    {
        int (*ptr)(int, int);
        int a, b, c;
        ptr = max;
        scanf("%d%d", &a, &b);
        c = (*ptr)(a,b);
        printf("a=%d, b=%d, max=%d", a, b, c);
        return 0;
    }

    extern:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义。此外extern也可用来进行链接指定。也就是说extern有两个作用,第一个,当它与"C"一起连用时,如: extern "C" void fun(int a, int b);则告诉编译器在编译fun这个函数名时按着C的规则去翻译相应的函数名而不是C++的,C++的规则在翻译这个函数名时会把fun这个名字变得面目全非,可能是fun@aBc_int_int#%$也可能是别的,因为C++支持函数的重载。
    第二,当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern int g_Int; 它的作用就是声明函数或全局变量的作用范围的关键字,其声明的函数和变量可以在本模块活其他模块中使用,记住它是一个声明不是定义!也就是说B模块(编译单元)要是引用模块(编译单元)A中定义的全局变量或函数时,它只要包含A模块的头文件即可,在编译阶段,模块B虽然找不到该函数或变量,但它不会报错,它会在连接时从模块A生成的目标代码中找到此函数。

    二、举例

    这里说一下背景,假如我这里做一个平台,我一套代码要交付到多个产品,然后结合产品代码进行使用,那么我做平台肯定不能为每个产品做一套代码,但是有时候同样一个功能,各个产品之间会出现差异,这个时候指针函数就派上用场了,我给各个产品提供一个指针函数定义的变量,然后各个产品将自己的实现函数挂接在上面,这样就屏蔽了各个产品的差异,甚至有些产品可以不挂接,那么我就判断一下,如果没挂接,我就给一个默认的实现就ok了(这部分在下面代码中没体现).

    代码如下:

    平台代码:

    #include<iostream>
    #include"lib_main.h"
    using namespace std;
    
    funcs g_hook_func;
    
    void hook_func_init()
    {
        g_hook_func.func1=NULL;
        g_hook_func.func2=NULL;
    }
    
    int main()
    {
        char name[10];
        int result = 0 ;
    
        memset(name,0,sizeof(name));
            
        hook_func_init();
    
        hook_func();//钩子挂接函数,多线程情况应该在产品侧挂接
    
        if(g_hook_func.func1 != NULL)
        {
            if(0 == g_hook_func.func1(name))
            {
                cout<<"err";
                return -1;
            }
        }
        if(g_hook_func.func2 != NULL)
        {
            result = g_hook_func.func2(1,2);
        }
    
        cout<<name<<"  "<<result<<endl;
        return 0;
    }
    
     
    
     

    平台头文件:

    #ifndef _LIB_MAIN_H_
    #define _LIB_MAIN_H_
    
    typedef struct func
    {
        int (*func1)(char * str);
        int (*func2)(int a,int b);
    }funcs;
    extern void hook_func();
    #endif

    产品1代码:

    #include"wlan.h"
    #include<iostream>
    using namespace std;
    
    static int getname(char * str)
    {
        if(NULL == str)
        {
            return 0;
        }
        //入参大小由调用者保证不越界
        str[0]='w';
        str[1]='l';
        str[2]='a';
        str[3]='n';
        str[4]='';
        return 1;
    }
    
    static int add(int a, int b)
    {
        return (a+b+3);
    }
    
    void hook_func()
    {
        g_hook_func.func1 = getname;
        g_hook_func.func2 = add;
    }

    产品1头文件代码:

    #ifndef _WLAN_H_
    #define _WLAN_H_
    
    
    typedef struct func
    {
        int (*func1)(char * str);
        int (*func2)(int a,int b);
    }funcs;
    
    extern funcs g_hook_func;
    
    #endif

    产品2代码:

    #include"ar.h"
    #include<iostream>
    using namespace std;
    
    static int getname(char * str)
    {
        if(NULL == str)
        {
            return 0;
        }
        //入参大小由调用者保证不越界
        str[0]='a';
        str[1]='r';
        str[2]='';
        return 1;
    }
    
    static int add(int a, int b)
    {
        return (a+b+1);
    }
    
    void hook_func()
    {
        g_hook_func.func1 = getname;
        g_hook_func.func2 = add;
    }

    产品2头文件:

    #ifndef _AR_H_
    #define _AR_H_
    
    
    typedef struct func
    {
        int (*func1)(char * str);
        int (*func2)(int a,int b);
    }funcs;
    
    extern funcs g_hook_func;
    
    
    #endif

    产品3代码:

    #include"fw.h"
    #include<iostream>
    using namespace std;
    
    static int getname(char * str)
    {
        if(NULL == str)
        {
            return 0;
        }
        //入参大小由调用者保证不越界
        str[0]='f';
        str[1]='w';
        str[2]='';
        return 1;
    }
    
    static int add(int a, int b)
    {
        return (a+b+2);
    }
    
    void hook_func()
    {
        g_hook_func.func1 = getname;
        g_hook_func.func2 = add;
    }

    产品3头文件:

    #ifndef _FW_H_
    #define _FW_H_
    
    
    typedef struct func
    {
        int (*func1)(char * str);
        int (*func2)(int a,int b);
    }funcs;
    
    extern funcs g_hook_func;
    
    #endif

    说明:

    1、上面的所有代码不能同时运行,想一下也应该知道,应该是一套lib和一套产品放在一个工程下运行。

    2、多线程条件下挂接钩子的函数hook_func应该在产品侧挂接,这样即使没有挂接,在lib侧也没有影响。

    3、平台和产品侧的结构都要进行声明,且要一致typedef struct func { int (*func1)(char * str); int (*func2)(int a,int b); }funcs;

        注意这里是声明,不是定义,所以不会分配内存,声明只是表示我这里现在有了这种类型(就像是说我这里有一个int一样)

  • 相关阅读:
    关于pipe管道的读写端关闭问题
    线性表的链式存储——C语言实现
    关于无法解析的外部符号 _main
    Tomcat域名与服务器多对多配置
    JavaScript基础
    Vue.js入门
    SpringBoot注解大全,收藏一波!!!
    数据库连接错误
    SpringBoot入门
    MyBatis插入并返回id技巧
  • 原文地址:https://www.cnblogs.com/bewolf/p/5905610.html
Copyright © 2020-2023  润新知