题
我有一个池管理模板类。当一个类对象被添加回池管理器,我想它重新回到它的初始状态。我想打电话给placment析构函数和placment构造函数就可以了,这样会将它完全复位它由池管理器给出了下一次。我试过许多方法来得到这个工作,但我难倒。下面是我曾尝试的例子。
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
}
我已经尝试了很多不同的语法和没有似乎工作。代码本身是跨平台的,从而应当编译使用(下MinGW的或Linux或Mac)和我仍然使用VS 2003窗口的gcc
解决方案
如何:
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...
}
其他提示
升压具有普尔库。它可能会更容易,只需使用他们,而不是写你自己的。
不隶属于 StackOverflow