SproutME 发表于 2015-8-27 14:31:49

C++实现回调的一种方式

#include <stdio.h>


class EpollInterface{
public:
    EpollInterface(){}
    virtual void epollEvent(){ printf("EpollInterface print\n");}
};

class Epoll : public EpollInterface{
public:
    Epoll(EpollInterface *parent){
      this->parent = parent;
    }

    void print(){
      printf("Epoll print!\n");
    }

    void epollEventHandle() {
      parent->epollEvent();
    }

private:
    EpollInterface *parent;
};

class UsrNeedEpoll : public EpollInterface{
public:
    UsrNeedEpoll() {
      usrEpoll = new Epoll(this);
    }

    void epollEvent() {
      printf("usrEpoll \n");
    }

    Epoll *usrEpoll;
};



int main()
{

    UsrNeedEpoll usr;

    usr.usrEpoll->epollEventHandle();
    return 0;
}

转角 发表于 2015-9-16 11:08:11

不要误导人,这不是回调

SproutME 发表于 2015-9-16 19:34:01

转角 发表于 2015-9-16 11:08 static/image/common/back.gif
不要误导人,这不是回调

你可以看清楚再说

转角 发表于 2015-9-16 20:58:59

这真不是回调。说它是个重写还不是重写,根本就是两个类里面互相包含了个对象而已。

SproutME 发表于 2015-9-18 21:21:57

转角 发表于 2015-9-16 20:58 static/image/common/back.gif
这真不是回调。说它是个重写还不是重写,根本就是两个类里面互相包含了个对象而已。

//
//main.cpp
//Lambda
//
//Created by Atropos on 15/9/2.
//Copyright © 2015年 Atropos. All rights reserved.
//

#include <iostream>
#include <string>
#include "ClassTemplate.hpp"

#include <pthread.h>
#include <unistd.h>

// 串口的事件处理接口
class Event_receive {
public:
    Event_receive() {}
   
    virtual int event(std::string str) = 0;
};

// 串口类定时一秒发一次;
class USART_EXAMPLE {
public:
    USART_EXAMPLE(Event_receive *ptr)
            : _usr(ptr)
    {
      pthread_create(&_pthread, NULL, task, this);
    }
   
    ~USART_EXAMPLE() {
      pthread_join(_pthread, NULL);
    }
   
private:
    // 使用者的指针用于回调接口
    Event_receive *_usr;
   
    // 线程号
    pthread_t _pthread;
   
private:
    // c调用c++的转换接口
    static void * task (void * ptr) {
      USART_EXAMPLE *func = (USART_EXAMPLE *)ptr;
      
      func->taskHandle();
      return 0;
    }
    // 定时发送
    void taskHandle() {
      while (true) {
            sleep(1);
            _usr->event("123");
      }
    }
};


class Usr
    // 实现事件接口
    : public Event_receive
{
public:
    Usr() : _usart(this) {}
private:
    // 实例化一个串口
    USART_EXAMPLE _usart;
   
private:
    // 事件处理函数供串口接收调用
    virtual int event(std::string str){
      std::cout << str << std::endl;
      return 1;
    }
};



int main(int argc, const char * argv[]) {
   
    Usr usr;
    return 0;
}

转角 发表于 2015-9-19 17:06:41

姑娘啊,你这个例子可以运行是真的,可能你把回调理解错了。你这个只是创建了线程,把对象传递给线程了,并在线程里面用这个对象调用它的函数。这个逻辑可以实现你的要求,但是不能叫它为回调。

SproutME 发表于 2015-9-19 19:05:30

转角 发表于 2015-9-19 17:06 static/image/common/back.gif
姑娘啊,你这个例子可以运行是真的,可能你把回调理解错了。你这个只是创建了线程,把对象传递给线程了,并 ...

怎么就不叫回调了呢~这个不叫callback 叫什么呢

转角 发表于 2015-9-20 21:50:19

这就是普通的一个对象调用成员函数。如果你创建线程时候那个参数传递的是函数地址,线程内直接使用这个函数地址的 那叫回调。
当别人提供一个回调接口的时候他们不可能跟你约定对象类型对吧,所以只提供函数入口地址就可以了。

SproutME 发表于 2015-9-21 20:22:47

转角 发表于 2015-9-20 21:50 static/image/common/back.gif
这就是普通的一个对象调用成员函数。如果你创建线程时候那个参数传递的是函数地址,线程内直接使用这个函数 ...

成员函数做为参数传递事非常复杂的;其实最好是用闭包来做,但是这样消耗太大了~;C++不像别的语言那样有内省机制,所以这种只是一种中庸方法,利用接口来约定处理事件,这种方式在Qt中也很常见!
页: [1]
查看完整版本: C++实现回调的一种方式