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't. ExampleTemplate
and ExampleTemplate
are two different, unrelated types. If you always have a switch over several options, use boost::variant
instead.
typedef boost::variant, Example> ExampleVariant;
ExampleVariant v;
switch (number) {
case 1: v = Example(); break;
case 2: v = Example(); break;
}
// here you need a visitor, see Boost.Variant docs for an example
Another way is to use an ordinary base class with virtual public interface, but I'd prefer variant
.
struct BaseExample {
virtual void do_stuff() = 0;
virtual ~BaseExample() {}
};
template
struct Example : BaseExample { ... };
// ..
BaseExample *obj;