从unordered_multimap中调用另一个类的函数?

问题描述 投票:0回答:1

这与我在上面发表的帖子有关:Creating an unordered_map of std::functions with any arguments。我现在已经把它扩展到了课堂上。所以我想说我有三个不同的课程。除了getVariable()setVariable(int)之外,这些类都有不同的方法。因此,对于这个例子,我们将它们分类为ClassAClassBClassC

我也有一个基类,我想用作我的驱动程序。基本上,如果我想在ClassAClassC之间设置变量,我会调用基类'setVariable函数。

#ifndef BASE_CLASS_HPP
#define BASE_CLASS_HPP

#include <unordered_map>
#include <functional>
#include <utility>
#include <string>
#include <any>

template<class A, class B>
class BaseClass
{
  public:
    BaseClass() { bindThem(); }

    std::pair<int,int> getValue()
    {
      // return classA and ClassB's values
    }

    void setValue(int newVal)
    {
      auto iter = functions.equal_range("setValue");
      std::any_cast<void(*)(int)>(mapIter->second)(newVal);
    }
  private:
    std::unordered_multimap<std::string,std::any> functions;

    void bindThem()
    {
      functions.emplace("setValue",&A::setValue);
      functions.emplace("setValue",&B::setValue);
      functions.emplace("getValue",&A::getValue);
      functions.emplace("getValue",&B::getValue);
    }

};

然后我在主要:

#include <iostream>

#include "baseClass.hpp"
#include "classA.hpp"
#include "classB.hpp"
#include "classC.hpp"

int main()
{
  ClassA a;
  ClassB b;
  ClassC c;

  c.setValue(20);

  BaseClass<ClassA,ClassB> base1;
  BaseClass<ClassA,ClassC> base2;

  base1.setValue(15);

  auto values = base1.getValues();


}

我可以将这些功能放在我的地图中,但是,当我尝试使用qazxsw poi时,我得不到任何回报。我也尝试过:

any_cast

但这也给了我一个必须使用的编译错误。*或 - > *我已经尝试了一切来让它编译,我真的不知道我做错了什么。我也意识到,如果我这样称呼它,那么我将无法访问B的std::any_cast<void(A::*)(int)>(mapIter->second)(newVal); 函数,因为我使用的是setVariable命名空间。

无论如何,我可以按照我想要的方式工作吗?我基本上试图修改这些类值,而不必制作这些类的任何副本,而是直接从此驱动程序中修改它们。

c++ templates c++17 function-pointers unordered-map
1个回答
0
投票

我仍然不太了解这种结构的目的,但这里有一个选项如何使它至少编译:

A's

请注意几件事:

  1. 我已经向BaseClass添加了成员​​,因为要调用成员函数,您需要调用一个对象。
  2. 我正在使用#include <unordered_map> #include <functional> #include <utility> #include <string> #include <any> template<class A, class B> class BaseClass { public: BaseClass() { bindThem(); } std::pair<int,int> getValue() { auto range = functions.equal_range("getValue"); return { (a.*std::any_cast<int(A::*)()>(*range.first))(), (b.*std::any_cast<int(B::*)()>(*range.second))() }; } void setValue(int newVal) { auto range = functions.equal_range("setValue"); (a.*std::any_cast<void(A::*)(int)>(*range.first))(newVal); (b.*std::any_cast<void(B::*)(int)>(*range.second))(newVal); } private: std::unordered_multimap<std::string,std::any> functions; void bindThem() { functions.emplace("setValue",&A::setValue); functions.emplace("setValue",&B::setValue); functions.emplace("getValue",&A::getValue); functions.emplace("getValue",&B::getValue); } A a; B b; }; class ClassA { public: void setValue(int){} int getValue() {return 0;} }; class ClassB { public: void setValue(int){} int getValue() {return 1;} }; int main() { BaseClass<ClassA, ClassB> x; x.setValue(3); auto i = x.getValue(); } 范围内的第一个和最后一个迭代器,但该范围内元素的顺序是实现定义的。因此,为了使事情有效,你需要注意区分哪个容器元素对应于类equal_range以及哪个类对应A
© www.soinside.com 2019 - 2024. All rights reserved.