我正在尝试声明许多Derived<T>
继承自 的模板化对象Base
,并将它们推回std::vector<Base*>
.
struct Base { ... };
template<typename T> struct Derived : Base { /* ctor(const string&) */ ... }
Derived<bool> online{"online"};
Derived<bool> official{"official"};
Derived<bool> noRotation{"no_rotation"};
Derived<bool> noBackground{"no_background"};
Derived<int> noSound{"no_sound"};
Derived<string> noMusic{"no_music"};
Derived<bool> blackAndWhite{"black_and_white"};
vector<Base*> configValues{&online,
&official,
&noRotation,
&noBackground,
&noSound,
&noMusic,
&blackAndWhite};
如您所见,代码很糟糕。有没有办法在不将vector
as a传递const&
给Derived<T>::Derived<T>(...)
构造函数的情况下自动执行此操作?
通过自动化,我的意思是避免对象名称的重复。我想std::vector
用我所有的Derived<T>
对象填充 a 而不必手动列出它们。
(宏已接受,但希望有更好的解决方案)