
23 Feb
2010
23 Feb
'10
1:20 p.m.
Steffen Roeber wrote:
My preferred way would be something like a factory:
class Factory
{
public:
template <class T>
static Base *createDerived<T>()
{
return new DerivedT<T>();
}
};
But how can I tell the factory which type to be used.
Base *derived = Factory::createDerived<sameAsBase> (base); //base is a Base *
You can't, since you lost that information. Even if you stored a type identifier for T, you couldn't get the type out of it, all you can do is call a function depending on that type identifier.
But Boost has some methods that by-pass some language constraints. I took a loot at something like 'typeof'.
typeof gives the static type of an object has deduced by the compiler, not the dynamic type. So it's useless in your case.