C++ 中的回调

Callbacks in C++

我正在做一个项目,我需要回调,因为它们使我们的工作更轻松,并帮助我们编写更清晰的代码。但是我没找到。

C++ 中是否有像 C# 中的委托那样的回调? 如果是,如何使用它们? 如果没有,还有其他选择吗?

我不懂 C#,也不懂委托。因此,我在这里添加我将用作回答您问题的基础(摘自 here):

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


我相信 C++ 无法直接替代 C# 委托,但它可以 std::function。您几乎可以在 std::function 中存储任何可调用对象,以便稍后调用它。动态定义可调用对象最方便的方法是使用 lambda 表达式:

#include <iostream>
#include <functional>
#include <vector>

struct foo {
    std::vector< std::function< void()>> callbacks;
    void call_all() {
        for (auto& f : callbacks){
            f();
        }
    }
};

void bar() {
    std::cout << "I am a callback\n";
}

int main() {
    foo f;
    f.callbacks.push_back(bar);
    f.callbacks.push_back( [](){ std::cout << "I am another callback"; });
    f.call_all();
}

我使用了最简单的情况,没有参数也没有 return 类型。例如 std::function<int(const Foo&)> 可以存储具有 const Foo& 参数和 return int 的可调用对象。在同一容器中存储具有不同签名的可调用文件更加复杂。

经过大量研究和您的帮助,现在我在 C++ 中得到了一个 delegate system 运行,几乎与我们在 C# 中看到的一样。这是完整的代码:-

#include <iostream>
#include <vector>
#include <string>

template <typename T, typename... Args>
class Delegate
{
    private:
        typedef T (*Function) (Args... args);
        std::vector <Function> functions;

    public :
        Delegate ()
        {
            functions = std::vector <Function> ();
        }

        ~Delegate ()
        {
            functions.clear ();
        }

        const void Invoke (const Args&&... args) const
        {
            for (Function func : functions)
            {
                func (args...);
            }
        }

        void Invoke (Args&&... args)
        {
            for (Function func : functions)
            {
                func (args...);
            }
        }

        const void operator () (const Args&&... args) const
        {
            for (Function func : functions)
            {
                func (args...);
            }
        }

        void operator () (Args&&... args)
        {
            for (Function func : functions)
            {
                func (args...);
            }
        }

        const void operator += (const Function func) const
        {
            functions.push_back (func);
        }

        void operator += (Function func)
        {
            functions.push_back (func);
        }

        const void AddListener (const Function func) const
        {
            functions.push_back (func);
        }

        void AddListener (Function func)
        {
            functions.push_back (func);
        }
};

void Log1 (const char* msg)
{
    std :: cout << msg << std :: endl;
    std :: cout << "Called by Log 1" << std :: endl;
}

void Log2 (const char* msg)
{
    std :: cout << msg << std :: endl;
    std :: cout << "Called by Log 2" << std :: endl;
}

int main ()
{
    Delegate <void, const char*> del;
    del += Log1;
    del += Log2;
    del ("Hello World!");
    std :: cin.get ();
}

template 中的第一个 typename 将成为 return type,其他(无限制)将成为可选的 parameters

任何人都可以在任何地方自由使用此代码