为什么std :: string没有虚拟析构函数? [英] Why doesn’t std::string have a virtual destructor?
问题描述
当我在一个涉及以给定语言定义句子的项目上工作时,我惊讶地发现 std :: string
析构函数不是虚拟的。这使得很难专门化这个类(我不得不创建一个包装)。为什么标准委员会决定让这个类不是虚拟的?
When I was working on a project that involved defining sentences in a given language, I was surprised to discover that std::string
destructor was not virtual. This made it a lot more difficult to specialize this class (I had to create a wrapper). Why did the standard committee decide to have this class not virtual?
在/usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/include/ g ++ - v4 / bits / basic_string.h,我们有:
in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/basic_string.h, we have:
template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string
{
...
/**
* @brief Destroy the string instance.
*/
~basic_string()
{ _M_rep()->_M_dispose(this->get_allocator()); }
推荐答案
我认为设计器暗示类不应该是子类。
It is by design. I think the designer is hinting that the class should not be sub-classed.
还要看这个:为什么不应该从c ++ std string类派生?
这篇关于为什么std :: string没有虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!