我有一個模板類(Node
是BST內的一個內部類)。現在是釋放內存的時候了;鑑於關鍵或價值(或兩者)可能是指針,我必須弄清楚如何釋放它們。如何刪除一個指針,如果它是一個模板值?
看到一個例子:
~Node(void)
{
if (is_pointer<TValue>(Value))
{
delete Value;
Value = NULL;
}
if (is_pointer<TComparable>(Key))
{
delete Key;
Key= NULL;
}
}
背後的is_pointer<T>
功能工作(從here兩者)的實施,但是隻要我按刪除任何項或值,我得到如下:
Error 13 error C2440: 'delete' : cannot convert from 'esc::Shader' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 131
Error 14 error C2440: 'delete' : cannot convert from 'esc::ShaderComparable' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 137
Error 12 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 130
我試過static_cast
,dynamic_cast
, reinterpret_cast
等,但這些都沒有工作。
什麼是一個好的解決方案?
附註:嘗試將null分配給值,然後** **刪除沒有意義。 '刪除值;值= NULL;'看起來會更好。 –
整頓。謝謝;) – zeboidlund