我使用以下命令编译 c++ 代码以禁用返回值。
g++ -fno-elide-constructors rvoptimazation.cpp -o test
但是 ./test 的输出是
10
10
10
13
0xbfdf0020
13
我对构造函数的最后一次调用感到困惑。谁能解释在运算符*中返回后哪一行代码将调用构造函数?提前致谢。
#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;
}