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

Invoking member of an object from another one by using `std::function`

本文关键字:调用 一个对象 对象 成员 std function 使用      更新时间:2023-10-16

我试图在对象之间创建一个通信系统,其中控制器对象保留其他对象的成员函数向量,并在需要时调用它们
我读了几篇关于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。)

查看您的代码,您最好存储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));
}

或者通过直接传递可转换为std::function<double(double, int)>的东西,将CallableCaller完全解耦:

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

在阅读了juancopanza的答案后,我重写了下面所附的代码。现在它按预期工作。

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();
    // ...
}
相关文章: