相关疑难解决方法(0)

具有参考成员的分配操作员

这是创建具有引用成员的赋值运算符的有效方法吗?

#include <new>

struct A
{
    int &ref;
    A(int &Ref) : ref(Ref) { }
    A(const A &second) : ref(second.ref) { }
    A &operator =(const A &second)
    {
        if(this == &second)
            return *this;
        this->~A();
        new(this) A(second);
        return *this;
    }
}
Run Code Online (Sandbox Code Playgroud)

它似乎编译并运行良好,但是当c ++倾向于表现出最不期望的未定义行为时,以及所有说它不可能的人,我认为我错过了一些问题.我错过了什么吗?

c++ reference

32
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

reference ×1