有时重新开始是件好事。在 C++ 中,我可以采用以下简单的操作:
{
T x(31, Blue, false);
x.~T(); // enough with the old x
::new (&x) T(22, Brown, true); // in with the new!
// ...
}
在作用域结束时,析构函数将再次运行,一切看起来都很好。(我们也可以说T
有点特殊,不喜欢被分配,更不用说交换了。)但有件事告诉我,破坏一切并重试并非总是没有风险。这种方法有可能会遇到问题吗?