<tfoot id='UFqSU'></tfoot>

    1. <i id='UFqSU'><tr id='UFqSU'><dt id='UFqSU'><q id='UFqSU'><span id='UFqSU'><b id='UFqSU'><form id='UFqSU'><ins id='UFqSU'></ins><ul id='UFqSU'></ul><sub id='UFqSU'></sub></form><legend id='UFqSU'></legend><bdo id='UFqSU'><pre id='UFqSU'><center id='UFqSU'></center></pre></bdo></b><th id='UFqSU'></th></span></q></dt></tr></i><div id='UFqSU'><tfoot id='UFqSU'></tfoot><dl id='UFqSU'><fieldset id='UFqSU'></fieldset></dl></div>
    2. <legend id='UFqSU'><style id='UFqSU'><dir id='UFqSU'><q id='UFqSU'></q></dir></style></legend>

      <small id='UFqSU'></small><noframes id='UFqSU'>

        <bdo id='UFqSU'></bdo><ul id='UFqSU'></ul>
    3. C++ 的面向对象回调?

      Object-Oriented Callbacks for C++?(C++ 的面向对象回调?)
      <legend id='Vpgky'><style id='Vpgky'><dir id='Vpgky'><q id='Vpgky'></q></dir></style></legend>

      <small id='Vpgky'></small><noframes id='Vpgky'>

        <tbody id='Vpgky'></tbody>

      • <tfoot id='Vpgky'></tfoot>
      • <i id='Vpgky'><tr id='Vpgky'><dt id='Vpgky'><q id='Vpgky'><span id='Vpgky'><b id='Vpgky'><form id='Vpgky'><ins id='Vpgky'></ins><ul id='Vpgky'></ul><sub id='Vpgky'></sub></form><legend id='Vpgky'></legend><bdo id='Vpgky'><pre id='Vpgky'><center id='Vpgky'></center></pre></bdo></b><th id='Vpgky'></th></span></q></dt></tr></i><div id='Vpgky'><tfoot id='Vpgky'></tfoot><dl id='Vpgky'><fieldset id='Vpgky'></fieldset></dl></div>
            • <bdo id='Vpgky'></bdo><ul id='Vpgky'></ul>
                本文介绍了C++ 的面向对象回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

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

                例如,Eiffel 语言具有代理"的概念,其工作方式或多或少是这样的:

                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);
                    }
                }
                

                输出将是:你好,来自酒吧!3 来自 Foo 的你好!

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

                所以 - 代理允许将成员函数封装到一个对象中,给它一些预定义的调用参数(来自 Foo 的 Hi),指定开放参数(?),并将其传递给其他一些可以调用它的对象稍后.

                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.

                由于 c++ 不允许在非静态成员函数上创建函数指针,因此在 c++ 中实现一些易于使用的东西似乎并不容易.我在 google 上找到了一些关于 C++ 中面向对象回调的文章,但是,实际上我正在寻找一些库或头文件,我可以简单地导入它们,以便我使用一些类似的优雅语法.

                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?

                谢谢!

                推荐答案

                在 C++ 中使用回调最面向对象的方式是调用接口的函数,然后传递该接口的实现.

                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
                "; }
                };
                
                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++ 的面向对象回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                <small id='fgjld'></small><noframes id='fgjld'>

                <tfoot id='fgjld'></tfoot>
                • <bdo id='fgjld'></bdo><ul id='fgjld'></ul>

                • <legend id='fgjld'><style id='fgjld'><dir id='fgjld'><q id='fgjld'></q></dir></style></legend>
                    <tbody id='fgjld'></tbody>

                      <i id='fgjld'><tr id='fgjld'><dt id='fgjld'><q id='fgjld'><span id='fgjld'><b id='fgjld'><form id='fgjld'><ins id='fgjld'></ins><ul id='fgjld'></ul><sub id='fgjld'></sub></form><legend id='fgjld'></legend><bdo id='fgjld'><pre id='fgjld'><center id='fgjld'></center></pre></bdo></b><th id='fgjld'></th></span></q></dt></tr></i><div id='fgjld'><tfoot id='fgjld'></tfoot><dl id='fgjld'><fieldset id='fgjld'></fieldset></dl></div>