使用`std :: function`从另一个对象的成员调用

时间:2013-07-31 09:44:50

标签: c++ oop c++11 callback std-function

我试图在对象之间创建一个通信系统,其中一个控制器对象保存其他对象的成员函数的向量,并在需要时调用/调用它们。 我阅读了几篇关于std::function用法的文章,但我找不到涵盖此用例的文章。

我的代码是:

#include <functional>
#include <Windows.h>

class Callable
{

};

class ClassA : public Callable
{
    public:
        double CallThis(double x, int y) const
        {
            MessageBoxW(NULL, L"CallThis() is called.", L"In ClassA", MB_ICONINFORMATION);
            return x + y;
        }
};

class ClassB : public Callable
{
    public:
        double CallMe(double x, int y) const
        {
            MessageBoxW(NULL, L"CallMe() is called.", L"In ClassB", MB_ICONINFORMATION);
            return x + y;
        }
};

class Caller
{
    public:
        void AddSubscriber( const Callable & ObjectToCall,
                            const std::function<double(const Callable &, double, int)> & Subscriber)
        {
            Subscribers.push_back(Subscriber);
            ObjectsToBeCalled.push_back(ObjectToCall);
        }
        void CallSubscribers()
        {
            for (size_t i=0; i<Subscribers.size(); i++)
            {
                Subscribers[i](ObjectsToBeCalled[i], 1.0, 2);
            }
        }
    private:
        std::vector<std::function<double(const Callable &, double, int)>> Subscribers;
        std::vector<const Callable &> ObjectsToBeCalled;
};

int APIENTRY wWinMain(  _In_        HINSTANCE   hInstance,
                        _In_opt_    HINSTANCE   hPrevInstance,
                        _In_        LPTSTR      lpCmdLine,
                        _In_        int         nCmdShow)
{
    // ...

    ClassA ObjectA;
    ClassB ObjectB;
    Caller ObjectCaller;
    ObjectCaller.AddSubscriber(ObjectA, &ClassA::CallThis);
    ObjectCaller.AddSubscriber(ObjectA, &ClassB::CallMe);
    Sleep(5000);
    ObjectCaller.CallSubscribers();

    // ...
}

奇怪的是,当我运行此代码时,我的代码中没有任何编译器错误,但是我从STL文件xmemory0vector收到错误。 错误是:

Error 1  xmemory0  527 error C2528: 'pointer' : pointer to reference is illegal
Error 2  xmemory0  528 error C2528: 'const_pointer' : pointer to reference is illegal
Error 3  xmemory0  561 error C2535: 'const Callable &(*std::allocator<_Ty>::address(const Callable &) throw() const)' : member function already defined or declared
Error 4  xmemory0  599 error C2528: '_Ptr' : pointer to reference is illegal
Error 5  xmemory0  604 error C2528: '_Ptr' : pointer to reference is illegal
Error 6  xmemory0  700 error C2528: 'pointer' : pointer to reference is illegal
Error 7  xmemory0  701 error C2528: 'const_pointer' : pointer to reference is illegal
Error 8  xmemory0  824 error C2535: 'const Callable &(*std::_Wrap_alloc<_Alloc>::address(const Callable &) const)' : member function already defined or declared
Error 9  xmemory0  890 error C2528: '_Ptr' : pointer to reference is illegal
Error 10 xmemory0  105 error C2528: 'abstract declarator' : pointer to reference is illegal
Error 11 xmemory0  107 error C2528: 'abstract declarator' : pointer to reference is illegal
Error 12 xmemory0  122 error C2528: 'pointer' : pointer to reference is illegal
Error 13 xmemory0  123 error C2528: 'const_pointer' : pointer to reference is illegal
Error 14 vector    773 error C2528: '_Pval' : pointer to reference is illegal
Error 15 vector   1184 error C2535: 'void std::vector<_Ty>::push_back(const Callable &)' : member function already defined or declared
Error 16 vector   1245 error C2535: 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert(std::_Vector_const_iterator<_Myvec>,_Ty)' : member function already defined or declared
Error 17 vector   1494 error C2528: '_Ptr' : pointer to reference is illegal
Error 18 vector   1658 error C2528: '_Pval' : pointer to reference is illegal

我做错了什么?如何运行此代码?

(我的IDE是Microsoft Visual Studio 2012。)

2 个答案:

答案 0 :(得分:2)

查看代码,看起来最好存储std::function<double(double, int)>,然后使用std::bind将对象绑定到成员函数。

template <typename F>
void AddSubscriber( const Callable & obj,
                    F subscriber)
{
    Subscribers.push_back(std::bind(subscriber, obj, _1, _2));
}

或者只是将CallableCaller完全分开,直接传递给std::function<double(double, int)>可转换的内容:

class Caller 
{
public:
  template <typename F>
  void AddSubscriber(F&& subscriber)
  {
     subscribers.emplace_back(std::forward<F>(subscriber));
  }
  void CallSubscribers()
  {
      for (const auto& subscriber : subscribers)
      {
          subscriber(1.0, 2);
      }
  }
 private:
  std::vector<std::function<double(double, int)> subscribers;
};

然后从来电方进行绑定。

Caller c;
ClassA a;
using namespace std::placeholders;
c.AddSubscriber(std::bind(&ClassA::CallThis, a, _1, _2));

答案 1 :(得分:0)

在阅读了juanchopanza的回答之后,我重写了我的代码,如下所示。现在它按预期工作。

class ClassA
{
    public:
        double CallThis(double x, int y) const
        {
            MessageBoxW(NULL, L"CallThis() is called.", L"In ClassA", MB_ICONINFORMATION);
            return x + y;
        }
};

class ClassB
{
    public:
        double CallMe(double x, int y) const
        {
            MessageBoxW(NULL, L"CallMe() is called.", L"In ClassB", MB_ICONINFORMATION);
            return x + y;
        }
};

class Caller
{
    public:
        void AddSubscriber(std::function<double(double, int)> Subscriber)
        {
            Subscribers.push_back(Subscriber);
        }
        void CallSubscribers()
        {
            for (const auto & Subscriber : Subscribers)
            {
                Subscriber(1.0, 2);
            }
        }
    private:
        std::vector<std::function<double(double, int)>> Subscribers;
};

int APIENTRY wWinMain(  _In_        HINSTANCE   hInstance,
                        _In_opt_    HINSTANCE   hPrevInstance,
                        _In_        LPTSTR      lpCmdLine,
                        _In_        int         nCmdShow)
{
    // ...

    ClassA ObjectA;
    ClassB ObjectB;
    Caller ObjectCaller;
    ObjectCaller.AddSubscriber(std::bind(&ClassA::CallThis, ObjectA, std::placeholders::_1, std::placeholders::_2));
    ObjectCaller.AddSubscriber(std::bind(&ClassB::CallMe,   ObjectB, std::placeholders::_1, std::placeholders::_2));
    Sleep(5000);
    ObjectCaller.CallSubscribers();

    // ...
}