在使用“placement new”时,建议显式调用构造函数和析构函数。
在这种情况下,在类的初始化部分中初始化的对象是否也会正确构造?
显式调用析构函数也一样吗?成员对象是否被正确销毁?
最佳答案
In the use of "placement new" it is suggested to call the constructor and destructor explicitly.
说“你显式调用构造函数”是不正确的,因为构造函数没有名称 ($12.1/1)。
In this case will the objects initialized in the initializer section of a class also get properly constructed?
是的。你为什么怀疑呢? Placment new 仅意味着 new
运算符不会分配任何内存,而是使用您在 placement new 中传递的内存来构造对象。该对象在您传递的内存中构建。
Same too with explicitly calling the destructor? Do the member objects get destroyed properly?
是的。
关于c++ - 当显式调用构造函数时,是否构造了初始化程序和成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4626738/