博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
回调函数
阅读量:7012 次
发布时间:2019-06-28

本文共 1223 字,大约阅读时间需要 4 分钟。

回调函数是你把函数地址传递过去,然后至于什么时候被调用,你并不需要知道,也不由你去调用。

#include <stdio.h>
//
#include <iostream>
//
using namespace std;
typedef 
void (*CALLBACK)(
int a,
int b);
class Example
{
private:
    
int m;
    
int n;
    
static CALLBACK func;
    
public:
    
void register_callback(CALLBACK fun,
int k,
int j);
    
void callcallback();
    
void calulate();
    
void increase(
int start,
int stop);
    
void decrease(
int start,
int stop);
};
CALLBACK Example::func=NULL;
void Example::register_callback(CALLBACK fun,
int k,
int j)
{
    func=fun;
    m=k;
    n=j;
}
void Example::callcallback()
{
    func(m,n);
}
void Example::increase(
int start,
int stop)
{
    
for(
int i= start; i< stop; i++)
        printf(
"
increase i = %d\n
", i);    
    
    callcallback();
}
void Example::decrease(
int start,
int stop)
{
    
for(
int i= start; i> stop; i--)
        printf(
"
decrease i = %d\n
", i);
    
    callcallback();
}
void Example::calulate()
{
    
if(m<n)
        increase(m,n);
    
else
        decrease(m,n);
}
void method_for_call(
int a, 
int b)
{
    printf(
"
\nsucceed finished the calulate work from %d to %d \n\n
",a, b);
}
int main(
void)
{
    Example ex;
    ex.register_callback(method_for_call,
2,
13);
    ex.calulate();
    printf(
"
1------------ 1\n\n
");    
    ex.register_callback(method_for_call,
23,
16);      
    ex.calulate();
    printf(
"
2------------2\n\n
");    

EOF

转载地址:http://nrttl.baihongyu.com/

你可能感兴趣的文章
tomcat发布web项目,支持域名
查看>>
js和Jquery获取选中select值和文本
查看>>
Linux系统排查1——内存篇
查看>>
Java实现注册邮箱激活验证
查看>>
数据库缓存
查看>>
mvc 数据验证金钱格式decimal格式验证
查看>>
常用的Web服务器
查看>>
UPW学习资料整理 .NET C# 转
查看>>
Oracle12c中新建用户
查看>>
分布式编译工具
查看>>
对我而言晦涩的递归
查看>>
React Native 从入门到原理
查看>>
iOS如何随意的穿插跳跃,push来pop去
查看>>
使用maven编译Java项目 http://www.tuicool.com/articles/YfIfIrq
查看>>
Strut2中的session和servlet中的session的区别
查看>>
自定义adapter实现listview双列显示
查看>>
MyBatis——实现关联表查询
查看>>
struts2的MVC模式
查看>>
cocos2d-x JS 复选按钮checkBox的单选与多选
查看>>
表格花式效果
查看>>