This is what I would like to do:
ExampleTemplate* pointer_to_template;
cin >> number;
switch (number) {
case 1:
pointer_to_template = new ExampleTempla
You can do something similar by having your template class derive from a regular class:
#include
#include
using namespace std;
class ExampleBase{
public:
virtual ~ExampleBase() {}
virtual string Get() = 0;
};
template
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(42);
break;
case 2:
base = new ExampleTemplate(3.14);
break;
default:
return 1;
}
cout << base->Get() << endl;
delete base;
return 0;
}