在其他构造函数中调用复制构造函数

eXX*_*XX2 8 c++ placement-new

#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
class A
{
public:
    std::string s;
    A()
    {
        s = "string";
        new(this)A(*this);
    }
};
int main()
{
    A a;
    std::cout<<a.s;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在输出中得到空字符串.C++标准对这种行为有何看法?

Bo *_*son 4

这里至少应该存在两个问题:

  • 您尝试使用 A 自身的副本来初始化 A
  • 在构造函数内部,A 尚未完全构造,因此您无法真正复制它

更不用说这new(this)本身就是可疑的。