我知道当构造函数失败时,已完成的成员对象将被销毁。没有内存泄漏。
我的问题是编译器是如何做到这一点的?编译器如何知道构造了什么成员?它有记录吗?在这种情况下,编译器真的会破坏所有内容吗?它如何保证这一点?
最佳答案
编译器如何做到这一点取决于编译器。
但是,是的,您可以保证任何构造的对象都将被破坏(以与构造它们相反的顺序)。 §15.2/2:
An object that is partially constructed or partially destroyed will have destructors executed for all of its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet begun execution. Should a constructor for an element of an automatic array throw an exception, only the constructed elements of that array will be destroyed. If the object or array was allocated in a new-expression, the matching deallocation function (3.7.3.2, 5.3.4, 12.5), if any, is called to free the storage occupied by the object.
关于c++ - 失败的构造函数如何翻转以销毁已完成的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2372291/