简体   繁体   English

如果指针是模板值,如何删除?

[英]How to delete a pointer if it is a template value?

I have a template class ( Node is an inner class within a BST). 我有一个模板类( Node是BST中的一个内部类)。 It's now time to free up the memory; 现在该释放内存了。 Given that either the key or the value (or both) may be pointers, I must figure out how to free them if they are. 鉴于键或值(或两者)都可能是指针,我必须弄清楚如何将它们释放。

See an example: 看一个例子:

~Node( void )
{
    if ( is_pointer< TValue >( Value ) )
    {
         delete Value;
         Value = NULL;
    }

    if ( is_pointer< TComparable >( Key ) )
    {
         delete Key;
         Key= NULL;
    }
}

The implementation behind the is_pointer< T > function works (Taken from here ), however as soon as I press delete on either Key or Value, I get the following: is_pointer< T >函数背后的实现可以正常工作(从此处 is_pointer< T > ),但是,只要在Key或Value上按Delete键,就会得到以下信息:

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

I've tried static_cast , dynamic_cast , reinterpret_cast , etc, but neither of these appear to work. 我已经尝试过static_castdynamic_castreinterpret_cast等,但是这些方法似乎都不起作用。

What's a good solution? 有什么好的解决方案?

I must figure out how to free them if they are.

Don't. 别。 Really- don't. 真的不是。 That's the user's problem- and he can supply a smart pointer if he wants this behaviour. 这就是用户的问题-如果他想要这种行为,他可以提供一个智能指针。 After all, what if I want to map non-owning pointers? 毕竟,如果我要映射非所有者指针怎么办? Or need a custom deleter? 还是需要自定义删除器?

Also, your code does not work because you compile the dead code if branches anyway, because you did not use a specialization. 同样,您的代码不起作用,因为if仍然分支, if您将编译死代码,因为您没有使用特殊化。

It looks like you are storing copies of elements of type T instead of pointers as commonly done. 看起来您正在存储T类型的元素的副本,而不是像通常那样存储指针。 If your declaration is T Value; 如果您的声明是T Value; than your Node class is normally not responsible for deleting Value object. 比您的Node类通常不负责删除Value对象。

This really depends on what kind of software you're working on here. 这实际上取决于您在这里使用哪种软件。 Unless it's some quick test and you're not going to reuse the code, then yes, don't bother distinguishing pointers from objects or arrays. 除非经过快速测试并且您不打算重复使用代码,否则是的,不要费心将指针与对象或数组区分开。 But if you're writing library code and you think your component will be reused by other people, then you should take care about cleaning after yourself. 但是,如果您正在编写库代码,并且认为自己的组件将被其他人重用,那么您应该注意自己进行清理。 STL vector has been doing this successfully since the dawn of times. 从时代开始,STL vector就已经成功地做到了这一点。 Last I saw the code they've been calling a Destroy function for (First,Last) elements of the vector with an value tag passed into the function as the third argument. 最后,我看到了他们一直在为向量的(First,Last)元素调用Destroy函数的代码,并将值标签作为第三个参数传递给该函数。 And if elements are just plane scalar pointers (meaning int* for instance), then destructors need not be called. 如果元素只是平面标量指针(例如,意味着int *),则不需要调用析构函数。 Obviously the real data has been allocated by the user and only addressed of the data has been stored in a vector, so it should not be deallocated. 显然,实际数据已由用户分配,并且仅将寻址的数据存储在向量中,因此不应取消分配。 But if it's objects that are stored in the vector (objects of a user-defined class for instance, class A, lets say), then destructors need to be called for each element in the vector ~A() and after all of them ran to the end, the contiguous memory chunk that used to store the elements should be deallocated through the use of vectors allocator. 但是如果对象是存储在向量中的(例如,用户定义类的对象,例如类A的对象),则需要为向量〜A()中的每个元素调用析构函数,并在它们全部运行之后最后,应该通过使用向量分配器来释放用于存储元素的连续内存块。 For more information you can easily open vectors implementation, it's all in the header file, look at ~vector() implementation and let that guide you. 有关更多信息,您可以轻松地打开vectors实现,它们全部在头文件中,查看〜vector()实现,并以此为指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM