I have the following template function defined at the header file.
template <class T> T* spawnEnemy(SpawnDirection dir);
My intention is to create a generic spawning function that accepts type of enemy as a template parameter and return the spawned object from the function.
The following is incorrect implementation but I use it to express what I want to achieve.
template <class T> T* ObjectSpawner::spawnEnemy(SpawnDirection dir)
{
if(_enemiesPool->isAnyFreeObjects())
{
T* enemy = NULL;
if(typeof(T*) == Zombie01) // This line is INCORRECT
enemy = dynamic_cast<T*>(_enemiesPool->popFreeObjectAndAddToActiveListForType(ZOMBIE));
else if ...
return enemy;
}
else
return NULL;
}
That is I need to be able to do type checking from T, so I can appropriately call function
popFreeObjectAndAddToActiveListForType(int type)
with correct value as its input.
So how can I do type checking in situation like this within template function?
EDIT: After got a suggestion from stephen lin and nvoigt, then I see that this design has a flaw but it's because I top it up from what I already. The easier is to let function have another type parameter and not involve with template but do need to cast to specific type later. Anyway, it's good to know a solution for this situation.