【发布时间】:2014-03-05 18:30:51
【问题描述】:
当我尝试std::string 时,我决定执行以下操作:
int main(int argc, char **argv)
{
std::string s = "Hello World";
s.~basic_string();
std::cout << s.c_str();
}
但是,它打印nothing,并且没有垃圾。但是,在 my basic_string 类中,当调用析构函数时,我得到了垃圾。 std::string 是如何处理这个问题的?我的使用allocator 和constructs、destroys、allocates 和deallocates,但它仍然不起作用。
注意:我不是在寻找我的类的解决方案,而是要了解std::string 是如何做到的。
【问题讨论】:
-
你知道这是非法的,对吧?
-
未定义的行为!你刚刚度过了幸运的一天
-
你为什么要关心你的对象被销毁后的状态?没有什么可修复的。
-
可能是因为Short string optimization。打开
标题并自己检查:) -
如果我没记错的话,你应该得到一个双重释放错误,因为析构函数被调用了两次。