请考虑以下代码:
class X {
int a;
public:
X(int a) : a(a){std::cout<<"in constructor";}
// X(const X& x) : a(x.a){std::cout<<"in copy constructor";}
X(const X& x) = delete;
X& operator=(const X& x) {
std::cout<<"in assignment";
a = x.a;
return *this;
}
};
int main(int argc, char *argv[]) {
X x = X(5);// error: `use of deleted function`
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:use of deleted function.但是,如果我取消注释复制构造函数并删除delete它,它工作正常,但不使用复制构造函数(输出是:) in constructor.
因此,如果X x = X(5);line在定义时不使用复制构造函数,为什么它在删除时会尝试使用它?
c++ ×1