我对此事做了一些研究,但尚未提出具体解决方案。我真的很想能够做到这一点:

    public delegate void VoidFloatCallback(float elapsedTime);
    public VoidFloatCallback OnEveryUpdate;
    public VoidFloatCallback OnNextUpdate;

    public virtual void Update(GameTime gameTime)
    {
        if (OnNextUpdate != null)
        {
            OnNextUpdate(gameTime);
            OnNextUpdate = null;
        }

        if (OnEveryUpdate != null)
        {
            OnEveryUpdate(gameTime);
        }

        this.OnUpdate(gameTime);
    }

但是,当然是在C++中。我发现只有一种解决方案可以为我提供这种功能。但此后已下线,但我将其重新发布到了http://codepad.org/WIVvFHv0。我发现的解决方案的唯一问题是,它不是现代的C++ 11代码,并且缺少lambda支持。

我知道我可以用
    std::function

但是唯一的问题是它不支持运算符“+=-===”。现在,我已经考虑过制作自己的事件类,并进行
    vector<std::function>

有一些模板,但是我发现std::function没有实现运算符==,因此我无法像C#那样使它看起来和感觉到。

无论如何,我的问题是这样的:

我想知道如何使用C++ 11实现这样的事件系统,或者甚至有可能实现。甚至即使您知道实现支持多个监听器的回调的更好/正确的方法(我也尽可能避免使用完整的Observer Pattern实现。)

更新#1

我对运营商的意图是:
    void some_func(float f) { /** do something with f **/ }
    void some_other_func(float f) { /** do something else with f **/ }
    OnNextUpdate += some_func();
    OnNextUpdate += some_other_func();

    OnNextUpdate(5.0f);
    // both some_func() and some_other_func() are called

    OnNextUpdate -= some_other_func();
    OnNextUpdate(5.0f);
    // only some_func() is called

最佳答案

C++函数对象机制与C#方法完全不同。特别是,功能对象基于值而不是引用。在C++中删除函数对象时可以识别的原因是该函数对象具有标识,即,调用它们的对象和调用成员函数的标识。同样,在C++中,不可能一次直接获取对象和成员函数的地址。

为了使代表系统能够删除功能,您可以创建类似于std::function<Signature>的东西,但是要使用多个功能,并且要求每个使用的功能都是EqualityComparable。以下是这种委托(delegate)系统的简单实现以及示例实现,这些实现用于成员函数的绑定(bind)器看起来像。存在许多明显的扩展机会,因为此实现仅用作演示。

#include <algorithm>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>

template <typename Signature>
struct delegate;

template <typename... Args>
struct delegate<void(Args...)>
{
    struct base {
        virtual ~base() {}
        virtual bool do_cmp(base* other) = 0;
        virtual void do_call(Args... args) = 0;
    };
    template <typename T>
    struct call: base {
        T d_callback;
        template <typename S>
        call(S&& callback): d_callback(std::forward<S>(callback)) {}

        bool do_cmp(base* other) {
            call<T>* tmp = dynamic_cast<call<T>*>(other);
            return tmp && this->d_callback == tmp->d_callback;
        }
        void do_call(Args... args) {
            return this->d_callback(std::forward<Args>(args)...);
        }
    };
    std::vector<std::unique_ptr<base>> d_callbacks;

    delegate(delegate const&) = delete;
    void operator=(delegate const&) = delete;
public:
    delegate() {}
    template <typename T>
    delegate& operator+= (T&& callback) {
        this->d_callbacks.emplace_back(new call<T>(std::forward<T>(callback)));
        return *this;
    }
    template <typename T>
    delegate& operator-= (T&& callback) {
        call<T> tmp(std::forward<T>(callback));
        auto it = std::remove_if(this->d_callbacks.begin(),
                                 this->d_callbacks.end(),
                                 [&](std::unique_ptr<base>& other) {
                                     return tmp.do_cmp(other.get());
                                 });
        this->d_callbacks.erase(it, this->d_callbacks.end());
        return *this;
    }

    void operator()(Args... args) {
        for (auto& callback: this->d_callbacks) {
            callback->do_call(args...);
        }
    }
};

// ----------------------------------------------------------------------------

template <typename RC, typename Class, typename... Args>
class member_call {
    Class* d_object;
    RC (Class::*d_member)(Args...);
public:
    member_call(Class* object, RC (Class::*member)(Args...))
        : d_object(object)
        , d_member(member) {
    }
    RC operator()(Args... args) {
        return (this->d_object->*this->d_member)(std::forward<Args>(args)...);
    }
    bool operator== (member_call const& other) const {
        return this->d_object == other.d_object
            && this->d_member == other.d_member;
    }
    bool operator!= (member_call const& other) const {
        return !(*this == other);
    }
};

template <typename RC, typename Class, typename... Args>
member_call<RC, Class, Args...> mem_call(Class& object,
                                         RC     (Class::*member)(Args...)) {
    return member_call<RC, Class, Args...>(&object, member);
}

// ----------------------------------------------------------------------------

void f(char const* str) { std::cout << "f(" << str << ")\n"; }
void g(char const* str) { std::cout << "g(" << str << ")\n"; }
void h(char const* str) { std::cout << "h(" << str << ")\n"; }

// ----------------------------------------------------------------------------

struct foo
{
    int d_id;
    explicit foo(int id): d_id(id) {}
    void bar(char const* str) {
        std::cout << "foo(" << this->d_id << ")::bar(" << str << ")\n";
    }
    void cbs(char const* str) {
        std::cout << "foo(" << this->d_id << ")::cbs(" << str << ")\n";
    }
};

// ----------------------------------------------------------------------------

int main()
{
    delegate<void(char const*)> d0;

    foo f0(0);
    foo f1(1);

    d0 += f;
    d0 += g;
    d0 += g;
    d0 += h;
    d0 += mem_call(f0, &foo::bar);
    d0 += mem_call(f0, &foo::cbs);
    d0 += mem_call(f1, &foo::bar);
    d0 += mem_call(f1, &foo::cbs);
    d0("first call");
    d0 -= g;
    d0 -= mem_call(f0, &foo::cbs);
    d0 -= mem_call(f1, &foo::bar);
    d0("second call");
}

关于C++中的像C#一样的代表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23973914/

10-09 18:19