Question

I have a pool manager template class. When a class object gets added back to the pool manager I would like to reset it back to it's initial state. I would like to call the placment destructor and placment constructor on it so it gets fully reset for the next time it is given out by the pool manager. I've tried many ways to get this to work but I'm stumped. Here is an example of what I have tried.

template <class T>
void PoolClass<T>::ReleaseToPool(T *obj)
{
     obj->~T();   //call destructor

     obj->T::T(); //call constructor
     //also tried new (obj)T(); //but this doesn't seem to work either

     //then misc code to add a pointer to the object
     //to my list of available objects for re-use later
}

I've tried a bunch of different syntaxes and none seem to work. The code itself is cross platform so should compile using gcc ( under mingw or linux or mac ) and for windows I'm still using vs 2003.

Was it helpful?

Solution

How about:

template <class T>
void PoolClass<T>::ReleaseToPool(T *obj)
{
    obj->~T();                  //call destructor
    obj = new ((void *)obj)T(); //call constructor

    // add a pointer to the object to the list...
}

OTHER TIPS

Boost has a Pool library. It might be easier to just use theirs instead of writing your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top