D
David Sanders
Hi,
I have a class depending on a template parameter:
template<int n>
class MyClass {
};
I want the template parameter to be chosen according to a variable,
e.g.
switch (method) {
case 1:
pointer = new MyClass<1>;
break;
case 2:
pointer = new MyClass<2>;
break;
The question is: what type should 'pointer' be?
The only thing that occurs to me is to inherit MyClass from a non-
templated abstract base class:
class BaseClass {
};
template<int n>
class MyClass : public BaseClass {
};
and then define
BaseClass* pointer;
This seems to work, but it requires me to define dummy pure virtual
versions in BaseClass of all the methods in MyClass, which seems like
something that should be able to happen automatically.
Is there a simpler / better solution to this problem?
[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]
Thanks and best wishes,
David.
I have a class depending on a template parameter:
template<int n>
class MyClass {
};
I want the template parameter to be chosen according to a variable,
e.g.
switch (method) {
case 1:
pointer = new MyClass<1>;
break;
case 2:
pointer = new MyClass<2>;
break;
The question is: what type should 'pointer' be?
The only thing that occurs to me is to inherit MyClass from a non-
templated abstract base class:
class BaseClass {
};
template<int n>
class MyClass : public BaseClass {
};
and then define
BaseClass* pointer;
This seems to work, but it requires me to define dummy pure virtual
versions in BaseClass of all the methods in MyClass, which seems like
something that should be able to happen automatically.
Is there a simpler / better solution to this problem?
[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]
Thanks and best wishes,
David.