I am stuck in a problem of conception in C++ :
I made a Pool template class which does mostly the same than std::list but stores only objects and calls itself the constructor with placement new and the destructor of stored object.
Then I made an ObjectAllocator class which has a getObjectWithClass template method. I want to have this class like an interface so I can have a PoolObjectAllocator subclass which acts as a bridge between ObjectAllocator and Pool template class.
But there is no way to create getObjectWithClass as virtual because this is a template method.
template <class T>
class Pool {
...
public:
T& getFreeObject();
...
}
class ObjectAllocator {
public:
template <class T> T* getObjectWithClass(); // I need this method to be virtual
};
class PoolObjectAllocator : public ObjectAllocator {
std::map<int, void *> pools;
public:
template <class T> T* getObjectWithClass() {
int type = T::GetType();
Pool<T> *pool;
if (this->pools.find(type) == this->pools.end()) {
pool = new Pool<T>();
pools[type] = pool;
} else {
pool = static_cast<Pool<T> *>(pools[type]);
}
return &pool->getFreeObject();
};
};
// Later in the program :
ObjectAllocator *objectAllocator = myObject.getObjectAllocator();
objectAllocator->getObjectWithClass<OneClass>();
// Because the above line call a non virtual method, the method called is ObjectAllocator::getObjectAllocator and not PoolObjectAllocator::getObjectAllocator even if the objectAllocator is a PoolObjectAllocator.
I can't find a way to get this working, somebody could help me ? Thanks