我有一個池管理器模板類。當一個類對象被添加回池管理器時,我想重置它回到它的初始狀態。我想調用它的placment destructor和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
}
我試過一堆不同的語法,似乎沒有工作。代碼本身是跨平臺,應該編譯使用gcc(下MinGW的或Linux或Mac)和Windows我仍然使用VS 2003
你有沒有試過這個http://stackoverflow.com/questions/362953/what-are-uses-of-the-c-construct-placement-new?即鑄造obj爲void * – Ismael 2009-01-28 19:18:59
是的,我試着將它鑄造成無效,然後做新(voidobj)T();在這個時候我懷疑VS2003是越野車,我需要升級。 – KPexEA 2009-01-28 19:51:12