follow コマンドを使用して C++ コードをコンパイルし、戻り値を無効にします。
g++ -fno-elide-constructors rvoptimazation.cpp -o test
しかし、./test の出力は
10
10
10
13
0xbfdf0020
13
コンストラクターの最後の呼び出しに混乱しています。operator* に戻った後、コードのどの行がコンストラクターを呼び出すか説明できますか? 前もって感謝します。
#include<iostream>
using namespace std;
class Rational{
public:
Rational(int x ,int y){
_a = x;
_b = y;
cout << __LINE__ << endl;
}
Rational(Rational const &t){
cout << __LINE__ << endl;
}
Rational operator*(Rational const &t){
Rational re = Rational(_a * t._a ,_b * t._b);
cout << &re << endl;
return re;
//return *this;
}
Rational get()
{
return *this;
}
public:
int _a ,_b;
};
int main()
{
Rational r1(1 ,2);
Rational r2(2 ,3);
r1 * r2;
// cout << &r3 << endl;
}