class X
{
public:
X() {};
X(int) : X() { throw std::exception(); }
X(double) { throw std::exception(); }
~X();
};
动态内存呢?通常构造函数中的异常意味着对象没有完全构造,因此内存被清理并且不会丢失。
但是上一个问题的论点是在委托完成后对象是完全构造(或完全初始化)的。这对回收内存有何影响?我希望内存仍然被清理!
int main()
{
new X(5); // new called
// delete called because delegate completed.
// I assume:
// Memory re-claimed (because constructor did not complete)
// I assume the C++11 standard adjusted to compensate.
// As a constructor did complete.
}
也比较过:
int main()
{
new X(5.0); // new called
// Delete **NOT** called
// Memory re-claimed (because constructor did not complete)
}
如果内存被清理,那么何时清理内存的定义需要从 C++03 规范中改变。行为如何改变?