c++ - 如果未定义赋值运算符,是否会调用复制构造函数?

标签 c++

假设foo类没有重载的赋值运算符。当为两个 foo 对象赋值 a = b; 时会发生什么? 选项有:

  1. 使用自动赋值运算符
  2. 复制构造函数

最佳答案

这取决于:

A a;
//this is not an assignment, it is equivalent to A b(a);
A b = a; //default copy constructor is called

A c;
//assignment
c = a; //default assignment operator is called

关于c++ - 如果未定义赋值运算符,是否会调用复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804823/

相关文章:

c++ - 实现非成员 IO 操作符

c++ - 将函数代码与预处理器指令混合是一种不好的做法吗?

c++ - socket() 和bind() 的行为如何?

c++ - 定义一个ostream修饰符c++

c++ - 条件编译

c++ - 为什么模板只能在头文件中实现?

c++ - LuaJIT FFI cdef 不理解 'class' ?

c++ - 请求输入文件,直到它在 C++ 中正确为止

c++ - C 编译错误 : stray '\200' in program and expected ')' before numeric constant

c++ - 在 C++ 中访问 union 元素