简体   繁体   中英

How can I overload the operator += to add two class objects

I am currently taking a course on C++ at my uni. We are discussing pointers and overloading operators at the moment. The task is about English length units (feet and inches).

I want to overload the operator+= . And I want the result to be something like this:

d3 = d1 += d2; // d1, d2, d3 are class objects. Lets say d1(3, 4) and d2(1, 3). So the result should be d3(4, 7)

First of all, there is a class called EnglishDistance (assume that all constructors are correctly created)

class EnglishDistance
{
private:
    int f, i;
public:
    EnglishDistance(int x, int y);
    EnglishDistance();

    // Do some other stuff
}

Inside this class, among other things, I have already implemented the overloading of operator+ (which works OK):

EnglishDistance operator+(EnglishDistance d) {
        EnglishDistance temp;

        temp.f = f + d.f;
        temp.i = i + d.i;

        // Some checks (if inches are >= 12 I will subtract 12 inches and add 1 feet)

        return temp;
}

And this is what I got so far about the operator+=

EnglishDistance& operator+=(EnglishDistance& d) {

        *this += d

        // This is the check I was talking about. Only in this instance I am applying it on a pointer.
        while (this->i >= 12) {
            this->i -= 12;
            this->f++;
        }

        return *this;
}

When I try to run this, I get an unhandled exception on Visual Studio (stack overflow), so obviously I have messed it up.

Can someone point out what's wrong with my code?

 *this += d

Ignoring the fact that this line won't compile (missing semicolon), it's also logically senseless; inside the function that implements this operation, you're invoking the operation again!

That'll result in an infinite chain of function calls, eventually smashing your stack to pieces and causing your program to crash.

Instead of just repeating how you want the function to be used, you actually need to tell the computer how it should be implemented .

I suspect you meant this:

this->f += d.f;
this->i += d.i;

(the this-> can be omitted, though)

As others have said, *this += d is calling EnglishDistance & operator+=(EnglishDistance& d), and then hitting *this += d, which is calling EnglishDistance & operator+= and on and on, until the stack overflows.

You really don't need "this" at all in any of your code thus far. I think it is confusing you anyway. Feel free to omit it completely and just use the names of the members directly.

Also, name your members appropriately with full meaningful names. Your future colleagues will thank you.

EnglishDistance & operator+=(EnglishDistance & rhs)
{
    // Convert to inches when adding
    m_inches += rhs.m_inches + rhs.m_feet * 12;

    // Now calculate the feet
    m_feet   += m_inches / 12;
    m_inches = m_inches % 12; // modulo operator gives remainder

    return *this;
}

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