C ++:从std :: map继承 [英] C++: Inheriting from std::map
问题描述
我想继承 std :: map
,但据我所知 std :: map
hasn任何虚拟析构函数。
I want to inherit from std::map
, but as far as I know std::map
hasn't any virtual destructor.
因此可以在我的析构函数中显式调用 std :: map
的析构函数, ?
Is it therefore possible to call std::map
's destructor explicitly in my destructor to ensure proper object destruction?
推荐答案
析构函数被调用,即使它不是虚拟的,但这不是问题。
The destructor does get called, even if it's not virtual, but that's not the issue.
如果您尝试通过指向 std :: map
。
使用composition而不是继承, std
容器不是要继承, 。
Use composition instead of inheritance, std
containers are not meant to be inherited, and you shouldn't.
我假设你要扩展 std :: map
的功能最低价值),在这种情况下,您有两个更好的,合法,选项:
I'm assuming you want to extend the functionality of std::map
(say you want to find the minimum value), in which case you have two far better, and legal, options:
1)如建议,代替:
template<class K, class V>
class MyMap
{
std::map<K,V> m;
//wrapper methods
V getMin();
};
2)免费功能:
namespace MapFunctionality
{
template<class K, class V>
V getMin(const std::map<K,V> m);
}
这篇关于C ++:从std :: map继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!