これは、 return ステートメントでのコンストラクターの呼び出しからのフォローアップの質問です。
これは、クラスでの演算子オーバーロードの楽しみです。
const Integer operator+(const Integer& IntObject)
{
cout << "Data : " << this->data << endl;
return Integer(this->data + IntObject.data);
}
そのような関数の戻り値の型における const の関連性は何ですか?
int main()
{
Integer A(1); //Create 2 object of class Integer
Integer B(2);
const Integer C = A + B; //This will work
Integer D = A + B; //This will also work
fun(A + B); //Will work
}
void fun(Integer F) {}
これは、NRVO が原因で返品ステップ中に一時が作成されない場合です。返されるオブジェクトは、呼び出し先のアドレスで直接構築されます。