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

来源:互联网 发布:c语言入门的书 编辑:程序博客网 时间:2024/04/30 19:52

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

首先说明确以下问题:


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 -- 01111 -- 11111 -- 21111 -- 31111 -- 41111 -- 51111 -- 61111 -- 71111 -- 81111 -- 91111 or 2222!2222 -- 02222 -- 12222 -- 22222 -- 32222 -- 42222 -- 52222 -- 62222 -- 72222 -- 82222 -- 91111 or 2222!1111 -- 5</span>

很明显一目了然。

0 0
原创粉丝点击