c中回调函数 c++类回调函数详解

~~~~我的生活,我的点点滴滴!!

首先说明确以下问题:


1 什么是回调函数?
2 有什么作用?


问题1: 什么是回调函数</span>  
通俗简单理解:回调函数就是函数指针

问题2:有什么用?
通俗简单理解:当你不知道在什么情况下要调用此函数,或者说如果你去卖店买东西,此东西没有,留下一个电话号码,当此物有的时候店员会打电话通知你,这个电话就是你的一个联系方式,通过他触发你知道,回调函数也是这样的,通过留下函数的一个地址,需要的时候调用就行。</span>  


先说c++ 类中的回调函数,对于c的我相信大家都知道,且网上很多。


c++中对于类的非静态成员函数,使用回调函数需要带上类名
class A
{
public:
int m_func;
int func();
};
声明:
int (A::*callone) = &A::m_func;</span>  
int (A::*callfunc) ()= &A::func(); </span>  
当你把类名去掉,你会发觉他和正常的函数指针一个样。


下面我们在来看看对于静态成员变量函数指针
class A
{
public:
static int m_func;
static int func();
};
声明: 
int *callone = &A::m_func;</span>  
int (*callfunc) ()= &A::func();


对于静态成员变量函数指针声明时不需要带上类,因为静态成员本身只属于类的,只不过赋值的时候需要带上类名限定,非静态需要带上,是他属于对象的。
下面来看个具体的实例,你就会明白了


// 头文件 
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QTime>

#define SLEEP(mtime)    \
    QTime hzftt;    \
    hzftt.start();  \
    while( hzftt.elapsed() < mtime )    \
    {\
        QApplication::processEvents();   \
    }\

namespace Ui {
class Widget;
}



class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    
private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;

    void mcallBack(int time, void (Widget::*phzf)(int));<span style="color: rgb(85, 255, 255); font-family: Arial, Helvetica, sans-serif;">//函数指针</span>


    void printhzf(int times);

};

#endif // WIDGET_H
</span>

//源文件

<span style="font-size:14px;color:#003333;">#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "start....";

    mcallBack(1000, &Widget::printhzf);

    qDebug() << "end....";
}

void Widget::mcallBack(int time, void (Widget::*phzf)(int))
{
    SLEEP(time);

    (this->*phzf)( 10 );//调用需要这样写,你把this与*phzf拆开了看,就明白了
}

void Widget::printhzf(int times)
{
    for(int i = 0 ;i < times; ++ i)
    {
        qDebug() << "hzf";
    }
}
</span>

主要看on_pushButton_clicked()里面调用,回调函数调用完了程序才继续往下执行,也就是说他并不是异步的,只不过触发是需要条件的

在来看另一个例子与c结合的:

<span style="font-size:14px;color:#003333;">#include <iostream>
#include <string>
 
using namespace std;
 
typedef void (*funchandler)();
 

void register_func(funchandler f)
{
    cout << "register_func" << endl;
    (*f)();
}
 
class A
{
public:
    A() : _val( 0 ) { cout << "create A..." << endl; }
    void test() { cout << "test..." << endl; }
    void test1() { cout << "test1..." << endl; }
    void test2() { cout << "test2..." << endl; }
    int val() { return _val; }
    static void test3() { cout << "test3..." << endl; }
    int _val;
private:
};
 
int main()
{
    A a;
    int ( A::*p_val ) = 0;
    p_val = &A::_val;
    cout << "a.*p_val: " << a.*p_val << endl;
   
    void (A::*p_func)();
    p_func = &A::test;
   
    a.test();
    (a.*p_func)();
   
    p_func = &A::test1;
    ( a.*p_func )();
    p_func = &A::test2;
    ( a.*p_func )();
   
    void (* pp_func)();
    pp_func = &A::test3;
    (*pp_func)();
   
    register_func( pp_func );
    return 0;
   
}</span>


这样c++的就说完了,对于c的,我不想多说什么,直接看例子,大家去体会

<span style="font-size:14px;color:#003333;">#include <stdio.h>

void print11(int len)
{
       printf("1111 -- %d\n", len);
}

void print22(int len)
{
       printf("2222 -- %d\n", len);
}

void callback(int times, void (* print)(int))
{
       int i;
       for (i = 0; i < times; ++i)
       {
              print(i);
       }
       printf("\n1111 or 2222!\n\n");
}

int main()
{
       callback(10, print11);
       callback(10, print22);
       print11(5);
       return 0;
}
</span>
输出结果:

<span style="font-size:14px;color:#003333;">1111 -- 0
1111 -- 1
1111 -- 2
1111 -- 3
1111 -- 4
1111 -- 5
1111 -- 6
1111 -- 7
1111 -- 8
1111 -- 9

1111 or 2222!

2222 -- 0
2222 -- 1
2222 -- 2
2222 -- 3
2222 -- 4
2222 -- 5
2222 -- 6
2222 -- 7
2222 -- 8
2222 -- 9

1111 or 2222!

1111 -- 5</span>

很明显一目了然。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值