二十、C语言中的面向接口编程(回调函数)

2018-06-12  本文已影响0人  木鱼_cc

函数三要素:名称、参数、返回值

思考:

//方法一
//定义一个数组类型
typedef int(ARRAY_INT_10_TYPE)[10];

//方法二
//直接定义一种数组指针
typedef int(*ARRAY_INT_10_TYPE_POINTER)[10];



int main(void)
{
   int array[10];
   ARRAY_INT_10_TYPE array2;
   ARRAY_INT_10_TYPE *array_p = NULL;//指向数组类型的指针
  ARRAY_INT_10_TYPE_POINTER array_p3 = NULL;

  *array;  //数组第一个元素int,array是数组的名字,首元素的地址
           //是一个存放int数组的第一个元素
  *array_p;//理解为存放数组的数组名!
           //是一个存放数组的数组的第一个元素

  for(int i = 0;i<10;i++){
     (*array_p)[i] = 10;
  }

   for(int i = 0;i < 10;i++){
       array[i] = 10;
       array2[i] = 10;

   }
   return 0;
}

1.函数类型语法基础

函数指针用于指向一个函数, 函数名是函数体的入口地址

  1. 可通过函数类型定义函数指针: FuncType* pointer;
  2. 也可以直接定义:type (*pointer)(parameter list);
    pointer为函数指针变量名
    type为指向函数的返回值类型
    parameter list为指向函数的参数类型列表
//定义一个函数
int func(int a,int b){
   cout<<"func 111"<<endl;
}

//定义一个函数指针
//C语言中决定一个函数类型
//返回值参数列表

typedef int(FUNC)(int ,int);//定义一个函数类型

typdef int(*FUNC_POINTER)(int,int);//定义一个函数指针类型

//当函数指针做为函数的参数,传递给一个被调用函数
//被调用函数就可以通过这个指针调用外部的函数
//这就形成了回调。
//如何将一个函数指针当做一个函数参数呢?
void my_function(FUNC *fp)
{
   fp(10,20);
}

void my_function2(FUNC_POINTER fp)
{
   fp(10,20);
}

//它是一个架构函数(和多态一样!)
void my_function3(int(*fp)(int,int))
{
   cout<<"1999年写的架构"<<endl;
   cout<<"固定业务1"<<endl;
   fp(10,20);
  cout<<"固定业务2"<<endl;
}

//2015年实现一个子函数
int my_new_function(int a,int b)
{
   cout<<a<<b<<endl;
   cout<<"2015年实现的新业务"<<endl;
   return 0;
}


int main(void)
{
   FUNC *p = func;
   FUNC_POINTER p2 = func;
   int (*fp)(int ,int) = func;

   p(10,20);
   (*p)(10,20);//以上两种写法等价

   p2(10,20);
   (*p2)(10,20);

   fp(10,20);
   
//2015年 实现一个子函数,再调用1999年的架构
   my_function3(my_new_function);
 //my_new_function 相当于int (*p)(int ,int)= my_new_function;
//所以这里的my_new_function不需要写参数!
   return 0;
}

2回调函数的优点

现在这几个函数是在同一个文件当中

int libfun(int (*pDis)(int a,intb))

是一个库中的函数,就只有使用回调了,通过函数指针参数将外部函数地址传入来实现调用。

函数 add 的代码作了修改,也不必改动库的代码,就可以正常实现调用便于程序的维护和升级。

//定义一个锦囊的方法,是一个指针,类似于C++的纯虚函数
typedef void(TIPS_FUNC)(void);


//写一个锦囊的结构体
struct tips
{
   char from[64];//这个锦囊是谁写的
   char to[64];   //写给谁的
   TIPS_FUNC *fp; //具体锦囊的内容
}


//写一个供调用的架构函数
void open_tips(struct tips *tp)
{
  cout<<"打开了锦囊"<<endl;
  cout<<"此锦囊是由"<<tp->from<<"写给"<<tp->to<<"的"<<endl;
  cout<<"内容是";
   tp->fp();//拆开锦囊,调用具体的方法
}


//实现一个锦囊,类似于实现一个纯虚函数
void tips_1(void)
{
   cout<<"111111111111111111111"<<endl;
}

void tips_2(void)
{
   cout<<"222222222222222222"<<endl;
}

void tips_3(void)
{
   cout<<"333333333333333333"<<endl;
}

void tips_4(void)//新加!!
{
   cout<<"444444444444444"<<endl;
}

struct tips * create_tips(char *from,char *to,TIPS_FUNC *fp)
{
  struct tips *tp = (struct tips*)malloc(sizeof(struct tips));
  if(tp == NULL)return -1;

  strcpy(tp->from,from);
  strcpy(tp->to,to);

 //注册回调函数
  tp->fp = fp;//给拥有函数指针的结构体 函数指针变量赋值 就是 注册回调函数

  return tp;
}

void destory_tips(struct tips *tp)
{
  if(tp != NULL)free tp;
}


int main(void)
{
  //孔明写锦囊
 struct tips *tp1 = create_tips("孔明","赵云",tip_1);
 struct tips *tp2 = create_tips("孔明","赵云",tip_2);
 struct tips *tp3 = create_tips("孔明","赵云",tip_3);

 struct tips *tp4 = create_tips("庞统","赵云",tip_4);


 //赵云拆锦囊
 open_tips(tp1);
 open_tips(tp2);
 open_tips(tp3);

 open_tips(tp4);

  destory_tips(tp1);
  destory_tips(tp2);
  destory_tips(tp3);
  destory_tips(tp4);
  return 0;
}


1.png

回调函数的本质:
提前做了一个协议的约定(把函数的参数、函数返回值提前约定)

上一篇 下一篇

猜你喜欢

热点阅读