为什么我们需要赋值运算符和副本构造函数?

时间:2019-12-21 20:59:58

标签: c++

虽然复制构造函数和赋值运算符都执行相同的复制工作,但为什么一个任务需要两种不同的方法。是由于不同的写入方式导致执行不同的操作吗?即,分数a = b将调用副本构造函数,而b = a在声明分数b之后将调用赋值运算符。

#include <iostream>

class fraction{
// some code

};

int main(){

fraction a;
fraction b = a; // invokes the copy constructor
b = a; // will invoke the assignment operator

}

0 个答案:

没有答案
相关问题