我是 C++ 的初学者,我不知道为什么 Rational r(2, 1) 的结果是“r == 0.5”,我发现 cotr 没有调用该operator()
函数。为什么?
class Rational {
public:
int numerator;
int denominator;
Rational(int a = 2, int b = 1)
{
cout<< "cotr"<<endl;
numerator = a;
denominator = b;
};
operator double() const
{
cout<< "()"<<endl;
return (double)this->denominator/this->numerator;
};
};
int main(int argc, const char * argv[])
{
Rational r(2, 1);
cout << "_____"<< endl;
cout << r << endl;
cout << r.numerator << " " << r.denominator << endl;
double d = 0.5*r;
cout << d;
return 0;
}