zoukankan      html  css  js  c++  java
  • 回调函数

    定义:通过函数指针来实现函数调用的东西,函数指针参考随笔《函数指针》

    好处:通过指针函数的形式,同一类型函数可以有不同实现,调用方可以选择调用不同实现。

    主要有两种方式

    方式一:通过命名方式

     1 #include <stdio.h>
     2 typedef int (*CallBackFun)(char *p);//typedef定义别名用法
     3 int fun(char *p)
     4 {
     5     printf("fun %s
    ",p);
     6     return 0;
     7 
     8 }
     9 
    10 int call (CallBackFun pCallBack ,char *p)
    11 {
    12    printf("call %s
    ",p);
    13    pCallBack(p);
    14    return 0;
    15     
    16 }
    17 
    18 int main(int argc,const char* argv[])
    19 {
    20 
    21   char *p = "hello";
    22   call(fun,p);
    23   return 0;
    24 
    25 }
    26 //执行结果如下:
    27 //call hello
    28 // fun hello

    方式二:直接通过函数指针

     1 #include <stdio.h>
     2 typedef int (*CallBackFun)(char *p);//typedef定义别名用法
     3 int fun(char *p)
     4 {
     5     printf("fun %s
    ",p);
     6     return 0;
     7 
     8 }
     9 
    10 int call (int (*ptr)(char *p) ,char *p)//不同方式一
    11 {
    12    printf("call %s
    ",p);
    13    (*ptr)(p);//不同方式一
    14    return 0;
    15     
    16 }
    17 
    18 int main(int argc,const char* argv[])
    19 {
    20 
    21   char *p = "hello";
    22   call(fun,p);
    23   return 0;
    24 
    25 }
    26 //执行结果如下:
    27 //call hello
    28 // fun hello
  • 相关阅读:
    期末实训学习认识SSH
    Hibernate 的认识
    action和domain的不同总结
    学习使用action属性来接受参数
    实现action的统配
    struts2学习
    路径问题--笔记
    学习C层
    innovus add_ndr rule
    innovus clock tree instance ccl cdb cwb等 名字命名含义
  • 原文地址:https://www.cnblogs.com/ycpkbql/p/9032192.html
Copyright © 2011-2022 走看看