析构函数的模板专门化
我正在构建一个 hashmap 类,它可以将不同类型的字符串键和整数、布尔值、字符串或指针作为其值,并且我想让它发挥作用。对于我使用它的程序,我创建了指针并将其传递到哈希图中。当我需要破坏地图时,问题就来了。如果哈希图的类型是指针,我需要在删除它的容器之前删除它(值)。
所以我现在的代码是这样的: 我有一个 hashNode** 抽屉,我将其用作二维数组来保存指向地图中 hashNodes 的指针。这些相同的指针也保存在另一个 hashNode** 数组中,该数组在将它们添加到映射时存储它们(为了轻松/快速地增长和复制哈希映射)。
template <typename V>
class str_map {
public:
// ...
virtual ~str_map() {
str_map<V>::~str_map();
}
// ....
};
然后我有很多这样的方法: 一个用于常规值:
template <>
str_map<int>::~str_map() {
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
一个用于指针:
template <>
str_map<str_map<int>*>::~str_map() {
for(int i=0; i < count && array[i]->val() != NULL; i++)
delete array[i]->val();
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
是否有另一种更好的方法来正确解构 str_map 类的实例,以便正确处理所有内存?或者至少有一种方法可以让这项工作发挥作用?
I'm building a hashmap class that can have string keys and ints, bools, strings or pointers of different types as its values, and I want it to work. For the program I'm using it for I create the pointer and pass it into the hashmap. The problem comes when I need to destruct the map. If the type for the hashmap is a pointer I need to delete it(the value) before I delete it's container.
so the code I have right now goes something like this:
I have a hashNode** drawers, which I use as a two dimensional array to hold pointer to hashNodes in the map. Those same pointers are also held in another hashNode** array, which stores them as they are added to map (for ease/speed of growing and copying the hashmap).
template <typename V>
class str_map {
public:
// ...
virtual ~str_map() {
str_map<V>::~str_map();
}
// ....
};
and then later I have a bunch of methods like these:
one for regular values:
template <>
str_map<int>::~str_map() {
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
and one for pointers:
template <>
str_map<str_map<int>*>::~str_map() {
for(int i=0; i < count && array[i]->val() != NULL; i++)
delete array[i]->val();
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
Is there another better way to deconstruct an instance of str_map class correctly so that all the memory is handled correctly? Or at least a way to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的容器应该处理值。就是这样,不多也不少。如果有人想插入指针,让他们。不要对他们可能指向或未指向的任何内容拥有所有权。
哈希映射的用户需要了解如何管理其内存的生命周期。它们应该使用智能指针,因此您的类只需复制它们,然后智能指针管理内存。
准则是管理一种资源,或者根本不管理任何资源。如果您管理多种资源,那么您就注定会失败。
我怀疑
delete array
应该是delete [] array;
。这意味着您确实应该使用 std::vector 。同样,要么管理一种资源,要么根本不管理。std::vector
管理一个资源,因此您不必这样做。等等。Your container should handle values. That's it, no more, no less. If someone wants to stick pointers in, let them. Don't take ownership of whatever they may or may not be pointing at.
It's up to the users of your hash map to know how to manage the lifetime of their memory. They should be using smart pointers, so your class just copies them around and the smart pointer manages the memory.
The guideline is manage one resource, or none at all. If you are managing more than one resource, you've set yourself up for failure.
I suspect
delete array
should bedelete [] array;
. What this means is you really should be usingstd::vector
. Again, either manage one resource or none at all.std::vector
manages one resource, so you don't have to. And so on.