面向对象的C ++的回调? [英] Object-Oriented Callbacks for C++?

查看:99
本文介绍了面向对象的C ++的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一些库可以让我轻松方便地在c ++中创建面向对象的回调?



例如,埃菲尔语具有代理的概念更多或更少的工作如下:

  class Foo {
public:
Bar * bar;

Foo(){
bar = new Bar();
bar-> publisher.extend(agent say(?,Hi from Foo!,?));
bar-> invokeCallback();
}

say(string strA,string strB,int number){
print(strA ++ strB ++ number.out);
}

}

class Bar {
public:
ActionSequence< string,int&出版商

Bar(){}

invokeCallback(){
publisher.call(Hi from Bar!
}
}

输出将是:
! 3嗨从Foo!



所以 - 代理允许将一个成员函数封装成一个对象,给它一些预定义的调用参数(Hi从Foo),指定开放



由于c ++不允许在非静态成员函数上创建函数指针,似乎不是微不足道的实现一些容易在c ++中使用。我发现一些文章与google的面向对象回调在c + +,然而,实际上我正在寻找一些库或头文件,我只是可以导入,允许我使用一些类似优雅的语法。



任何人都有我的提示?



谢谢!

解决方案>

在C ++中使用Callback的最常用的方法是调用接口的函数,然后传递该接口的实现。

  #include< iostream> 

类接口
{
public:
virtual void callback()= 0;
};

class Impl:public Interface
{
public:
virtual void callback(){std :: cout< Hi from Impl \\\
; }
};

类用户
{
public:
用户(接口和newCallback):myCallback(newCallback){}

void DoSomething {myCallback.callback(); }

private:
接口&我的回
};

int main()
{
Impl cb;
用户用户(cb);
user.DoSomething();
}


Is there some library that allows me to easily and conveniently create Object-Oriented callbacks in c++?

the language Eiffel for example has the concept of "agents" which more or less work like this:

class Foo{
public:
    Bar* bar;

    Foo(){
        bar = new Bar();
        bar->publisher.extend(agent say(?,"Hi from Foo!", ?));
        bar->invokeCallback();
    }

    say(string strA, string strB, int number){
        print(strA + " " + strB + " " + number.out);
    }

}

class Bar{
public:
    ActionSequence<string, int> publisher;

    Bar(){}

    invokeCallback(){
        publisher.call("Hi from Bar!", 3);
    }
}

output will be: Hi from Bar! 3 Hi from Foo!

So - the agent allows to to capsule a memberfunction into an object, give it along some predefined calling parameters (Hi from Foo), specify the open parameters (?), and pass it to some other object which can then invoke it later.

Since c++ doesn't allow to create function pointers on non-static member functions, it seems not that trivial to implement something as easy to use in c++. i found some articles with google on object oriented callbacks in c++, however, actually i'm looking for some library or header files i simply can import which allow me to use some similarily elegant syntax.

Anyone has some tips for me?

Thanks!

解决方案

The most OO way to use Callbacks in C++ is to call a function of an interface and then pass an implementation of that interface.

#include <iostream>

class Interface
{
  public:
  virtual void callback() = 0;
};

class Impl : public Interface
{
  public:
  virtual void callback() { std::cout << "Hi from Impl\n"; }
};

class User
{
  public:
  User(Interface& newCallback) : myCallback(newCallback) { }

  void DoSomething() { myCallback.callback(); }

  private:
  Interface& myCallback;
};

int main()
{
  Impl cb;
  User user(cb);
  user.DoSomething();
}

这篇关于面向对象的C ++的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆