简体   繁体   中英

c++ operator overloading not working as expected

Here is the whole class (copy/paste should work):

#include <cstdio>
#include <iostream>
using namespace std;

class Rational
{
    int _n = 0;
    int _d = 1;
public:
    Rational (int numerator = 0, int denominator = 1) : _n(numerator), _d(denominator) {};
    Rational (const Rational & rhs) : _n(rhs._n), _d(rhs._d) {};
    ~Rational();
    int numerator() const {
        return _n;
    };
    int denominator() const {
        return _d;
    };
    Rational & operator = (const Rational &);

};

Rational operator + (const Rational & lhs, const Rational & rhs)
{
    return ((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator());
}

Rational::~Rational()
{
    _n = 0;
    _d = 1;
}

std::ostream & operator << (std::ostream & o, const Rational & r)
{
    if (r.denominator() == 1) {
        return o << r.numerator();
    } else {
        return o << r.numerator() << '/' << r.denominator();
    }
}


int main()
{
    Rational a = 7;
    cout << "a is: " << a << endl;

    Rational b(5,3);
    cout << "b is: " << b << endl;

    cout << a << " + " << b << " = " << a + b << endl;
    cout << 14 << " + " << b << " = " << 14 + b << endl << endl;

    return 0;
}

I want to use non-member operator overload. Code compiles but it displays wrong results. The last two lines should display:

7 + 5/3 = 26/3

14 + 5/3 = 47/3

but instead displays:

7 + 5/3 = 3

14 + 5/3 = 3

The problem is most likely this:

Rational operator + (const Rational & lhs, const Rational & rhs)
{
    return ((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator());
}

because it works properly if I change it to create object first and then return:

Rational operator + (const Rational & lhs, const Rational & rhs)
{
    Rational r((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator());
    return r;
}

I would expect implicit conversion and both solutions give the same results. Could someone explain to me what is the difference here?

return statement contains comma operator invocation discarding result of first expression (that was supposed to be numerator) and invoking constructor with just one paramter. You should use proper initialization syntax:

return Rational
(
    (lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator())
,   lhs.denominator() * rhs.denominator()
);

or

return
{
    (lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator())
,   lhs.denominator() * rhs.denominator()
};

or (better)

return Rational
{
    (lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator())
,   lhs.denominator() * rhs.denominator()
};

This line causes your bug:

return ((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator());

The problem is that it gets evaluated as a whole, which gives you an integer. This integer is then fed to the implicitly invoked constructor of your Rational class. Note that this line uses the comma-operator. For code like a, b it first evaluates a , discards the result and then evaluates b and keeps that result as result of the overall expression. That's not what you intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM