どちらがどのような状況で呼び出されるかを理解しています...
Sample a;
Sample b = a; //calls copy constructor
Sample c;
c = a; //calls assignment operator
私の質問は、なぜこれら2つの異なるものが存在するのですか? 2 つのうちの 1 つだけが両方の状況を処理できないのはなぜですか?
どちらがどのような状況で呼び出されるかを理解しています...
Sample a;
Sample b = a; //calls copy constructor
Sample c;
c = a; //calls assignment operator
私の質問は、なぜこれら2つの異なるものが存在するのですか? 2 つのうちの 1 つだけが両方の状況を処理できないのはなぜですか?
基本的に、それらはさまざまな状況で使用されますが、多くは正解です。コピーコンストラクターも使用されている場所を明確にするために何かを追加したいだけです:
例えば:
void test(Fraction in){
//Do something here
}
Fraction test2(){
Fraction a(1, 2);
//Do something here
return a; // construct a return value by copy a
}
int main()
{
Fraction a(2, 3);
Fraction b(a); //construct b by copy a
Fraction c=a; //construct c by copy a
test(a); //construct in by copy a
}