Here is the code snippet. In the definition of class Rational, I specified convertion constructor explicit so that I can prevent a int converted implicitly into a Rational object. But to my surprise, when I call Rational r = r1*2, 2 is still converted to a Rational object. Why?
Code:class Rational { public: Rational(int x = 0, int y = 1) { num = x; den = y; } explicit Rational(const int& x) { } const Rational& operator*(const Rational& rhs) { num*=rhs.num; den*=rhs.den; return *this; } private: int num; int den; }; int main() { Rational r1(2, 3); Rational r = r1*2; return 0; }


Reply With Quote


Bookmarks