c++ - 在 C++ 中是否可以遍历抽象类的所有子类?

标签 c++ macros template-meta-programming

我在 C++ 中有一个带有多个子类的抽象类。

是否可以通过宏或模板元编程以某种方式做这样的事情:

foreach subclass of Base:
  mymap[subclass::SOME_CONSTANT] = new subclass();

最佳答案

不,你不能。

显然,您想要的是一个工厂(或者可能是抽象工厂)。

在 C++ 中,您设置工厂类并注册构建器。

class FooFactory
{
public:
  typedef std::function<Foo*()> Builder;

  /// returns true if the registration succeeded, false otherwise
  bool Register(std::string const& key, Builder const& builder) {
    return map.insert(std::make_pair(key, builder)).second;
  }

  /// returns a pointer to a new instance of Foo (or a derived class)
  /// if the key was found, 0 otherwise
  Foo* Build(std::string const& key) const {
    auto it = _map.find(key);
    if (it == _map.end()) { return 0; } // no such key
    return (it->second)();
  }

private:
  std::map<std::string, Builder> _map;
};

您可以创建该工厂的单例,以在库加载期间注册派生类,这对于类似插件的架构非常方便:

FooFactory& GetFooFactory() { static FooFactory F; return F; }

你可以准备一个方便的构建器:

template <typename Derived>
Foo* fooBuilder() { return new Derived(); }

然后人们期望在工厂中注册他们的派生类:

static const bool registeredBar =
    GetFooFactory().Register("Bar", fooBuilder<Bar>);

注意:工厂应该是单例远非强制性的,尽管它在这里并不那么邪恶,因为一旦库的加载结束它就会保持不变。

注意:对于正确的插件架构,您需要使用 RAII(而不是 bool)来处理库卸载时的注销。不过这种情况要少得多。

关于c++ - 在 C++ 中是否可以遍历抽象类的所有子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450742/

相关文章:

c++ - 如何快速从已排序的 vector 中获取已排序的子 vector

c++ - 构造函数委托(delegate)与默认参数

c++ - SCons out of source build 仅适用于可执行程序,不适用于目标文件

asp.net - Kentico 9 购物车 ECommerceContext 金额在宏和布局/.NET 之间不一致

c++ - 在原始字符串中扩展宏

metaprogramming - "meta"这个词是什么意思?

C++ - Clutter 1.0 - 从线程调用函数导致段错误

c - C 宏上的警告过多

c++ - 将元组转换为模板中的结构

c++ - C++17/C++2a 编译时的哈希类型