【发布时间】:2016-08-20 17:56:45
【问题描述】:
考虑这段代码
enum Types
{
t1,
t2
};
struct Base
{
Types typeTag;
Base(Types t) : typeTag(t){}
};
template<typename T>
struct Derived : Base
{
using Base::Base;
T makeT() { return T(); }
};
int main()
{
Base *b = new Derived<std::string>(t1);
auto d = getDerivedByTag(b); // How ??
d->makeT();
return 0;
}
是否可以在运行时通过 Base::typeTag 值恢复派生类型参数?显然,需要一些外部预先准备好的映射,但我无法弄清楚确切的方法。
【问题讨论】:
标签: c++ templates inheritance