Inheritance and template method in C++ -
i stuck in problem of conception in c++ :
i made pool template class same std::list stores objects , calls constructor placement new , destructor of stored object.
then made objectallocator class has getobjectwithclass template method. want have class interface can have poolobjectallocator subclass acts bridge between objectallocator , pool template class.
but there no way create getobjectwithclass virtual because template method.
template <class t> class pool { ... public: t& getfreeobject(); ... } class objectallocator { public: template <class t> t* getobjectwithclass(); // need method 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 program : objectallocator *objectallocator = myobject.getobjectallocator(); objectallocator->getobjectwithclass<oneclass>(); // because above line call non virtual method, method called objectallocator::getobjectallocator , not poolobjectallocator::getobjectallocator if objectallocator poolobjectallocator.
i can't find way working, me ? thanks
Comments
Post a Comment