Templates are expanded at compile-time, so your problem is really just the same as the following:
struct A_float { // struct is easier when everything's public
A(float v) : var(v) {} // (use the ctor-initializer please!)
~A() {}
float var;
}; // don't forget the semicolon
struct A_int {
A(int v) : var(v) {}
~A() {}
int var;
};
int main() {
WhatType* object; // What type here?
if (/* something*/)
object = new A_float(0.2f);
else
object = new A_int(3);
}
Hopefully if you saw the above code, you'd think (as well as "maybe I should be using templates") "I am going to need a common base class for this, or else I'll refactor".
When you generate the two types at compile-time using a class template, this conclusion is the same.
- I'd recommend the refactoring, going for a solution like Puppy's; creating an inheritance hierarchy just to work around a program logic flow flaw is programming backwards!