1

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.

KPexEA
  • 16,560
  • 16
  • 61
  • 78
  • Have you tried this http://stackoverflow.com/questions/362953/what-are-uses-of-the-c-construct-placement-new? ie Casting obj to void* – Ismael Jan 28 '09 at 19:18
  • Yes I tried casting it to void and then doing new(voidobj)T(); At this time I suspect VS2003 is buggy and I need to upgrade. – KPexEA Jan 28 '09 at 19:51

2 Answers2

3

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...
}
e.James
  • 116,942
  • 41
  • 177
  • 214
2

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

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79