Nat*_*han 18 c++ generics templates pointers
这就是我想做的事情:
ExampleTemplate* pointer_to_template;
cin >> number;
switch (number) {
case 1:
pointer_to_template = new ExampleTemplate<int>();
break;
case 2:
pointer_to_template = new ExampleTemplate<double>();
break;
}
pointer_to_template->doStuff();
Run Code Online (Sandbox Code Playgroud)
这不会编译,因为在声明指针时必须指定模板类型.(ExampleTemplate* pointer_to_template应该是ExampleTemplate<int>* pointer_to_template.)不幸的是,我不知道模板的类型,直到它在switch块中声明.对于这种情况,最好的解决方法是什么?
Cat*_*lus 17
你不能.ExampleTemplate<int>并且ExampleTemplate<double>是两种不同的,不相关的类型.如果您始终切换多个选项,请boost::variant改用.
typedef boost::variant<Example<int>, Example<double>> ExampleVariant;
ExampleVariant v;
switch (number) {
case 1: v = Example<int>(); break;
case 2: v = Example<double>(); break;
}
// here you need a visitor, see Boost.Variant docs for an example
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用具有虚拟公共接口的普通基类,但我更喜欢variant.
struct BaseExample {
virtual void do_stuff() = 0;
virtual ~BaseExample() {}
};
template <typename T>
struct Example : BaseExample { ... };
// ..
BaseExample *obj;
Run Code Online (Sandbox Code Playgroud)
您可以通过使模板类派生自常规类来执行类似的操作:
#include<iostream>
#include<sstream>
using namespace std;
class ExampleBase{
public:
virtual ~ExampleBase() {}
virtual string Get() = 0;
};
template<typename T>
class ExampleTemplate : public ExampleBase{
private:
T data;
public:
ExampleTemplate(T t) : data(t){}
string Get(){
stringstream s; s << data;
return s.str();
}
};
int main(){
ExampleBase *base;
int number;
cout << "> " << flush; cin >> number;
switch(number){
case 1:
base = new ExampleTemplate<int>(42);
break;
case 2:
base = new ExampleTemplate<double>(3.14);
break;
default:
return 1;
}
cout << base->Get() << endl;
delete base;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你想要做的是不可能的.这是因为你的ExampleTemplate类本身不存在,只有在你将它与一个类型相关时才存在.
您可以使用继承获得该行为:
GeneralExampleTemplate(不是模板类).ExampleTemplate<T>继承GeneralExampleTemplate.GeneralExampleTemplate指针并用(例如)分配它ExampleTemplate<int>.