C ++运算符重载显示奇怪的行为

时间:2014-02-20 12:55:58

标签: c++

我写了一个简单的程序

#include <iostream>

using namespace std;

class Interval
{
public:
    Interval(int m_input, int s_input)
    {
        minutes =  m_input + s_input / 60;
        seconds = s_input % 60;
    }   

    void Print()            const
    {
        cout << minutes << " : " << seconds << endl;
    }

    int GetSeconds()        const
    {
        return 60 * minutes + seconds;
    }

    Interval& operator+(const Interval& rhs)
    {
        minutes = this->minutes + rhs.minutes;
        seconds = this->seconds + rhs.seconds;
        return Interval(minutes, seconds);
    }

    Interval& operator-(const Interval& rhs)
    {
         int totalSeconds = this->GetSeconds() - rhs.GetSeconds();
         return Interval(0, totalSeconds);
    }

private:
    int minutes;
    int seconds;
};

int main()
{
    Interval t1(7, 45);
    t1.Print();
    Interval t2(3, 75);
    t2.Print();
    Interval t3 = t1 + t2;
    t3.Print();
    Interval t4 = t3 - t1;
    t4.Print();
    return 0;
} 

它的输出是 7:45 4:15 12:0 0:0&lt; -----------请注意这个。

但是在我们的程序中进行一些更改,比如

#include <iostream>

using namespace std;

class Interval
{
public:
    Interval(int m_input, int s_input)
    {
        minutes =  m_input + s_input / 60;
        seconds = s_input % 60;
    }   

    void Print()            const
    {
        cout << minutes << " : " << seconds << endl;
    }

    int GetSeconds()        const
    {
        return 60 * minutes + seconds;
    }

    Interval& operator-(const Interval& rhs)
    {
         int totalSeconds = this->GetSeconds() - rhs.GetSeconds();
         return Interval(0, totalSeconds);
    }

private:
    int minutes;
    int seconds;

    friend Interval& operator+(const Interval& lhs, const Interval& rhs);
};

Interval& operator+(const Interval& lhs, const Interval& rhs)
{
    int minutes = lhs.minutes + rhs.minutes;
    int seconds = lhs.seconds + rhs.seconds;
    return Interval(minutes, seconds);
}

int main()
{
    Interval t1(7, 45);
    t1.Print();
    Interval t2(3, 75);
    t2.Print();
    Interval t3 = t1 + t2;
    t3.Print();
    Interval t4 = t3 - t1;
    t4.Print();
    return 0;
} 

显示输出 7:45 4:15 12:0 4:15 ---&gt;看看这个为什么我不知道:(请放飞。

感谢您的帮助

维沙尔

2 个答案:

答案 0 :(得分:4)

您的第一个版本会修改成员变量,但不应该:

Interval& operator+(const Interval& rhs)
{
    minutes = this->minutes + rhs.minutes;
    seconds = this->seconds + rhs.seconds;
    return Interval(minutes, seconds);
}

对修改后的Interval执行后续减法操作。

您的第二个版本不会对成员变量进行此修改:

Interval& operator+(const Interval& lhs, const Interval& rhs)
{
    int minutes = lhs.minutes + rhs.minutes;
    int seconds = lhs.seconds + rhs.seconds;
    return Interval(minutes, seconds);
}

此外,在这两种情况下,您都会返回对局部变量的引用,这是错误的。

答案 1 :(得分:1)

这些运营商

Interval& operator+(const Interval& rhs)
{
    minutes = this->minutes + rhs.minutes;
    seconds = this->seconds + rhs.seconds;
    return Interval(minutes, seconds);
}


Interval& operator-(const Interval& rhs)
{
     int totalSeconds = this->GetSeconds() - rhs.GetSeconds();
     return Interval(0, totalSeconds);
}

无效。它们返回对操作符完成后将被销毁的本地对象的引用。

同样在第一个运算符中,您更改第一个操作数的数据成员

    minutes = this->minutes + rhs.minutes;
    seconds = this->seconds + rhs.seconds;

因为在这些陈述中,分和秒相当于此 - &gt;分钟和此 - >&gt;秒

按以下方式更改

Interval  operator +(const Interval& rhs) const
{
    int minutes = this->minutes + rhs.minutes;
    int seconds = this->seconds + rhs.seconds;
    return Interval( minutes, seconds);
}

Interval operator -(const Interval& rhs) const
{
     int totalSeconds = this->GetSeconds() - rhs.GetSeconds();
     return Interval(0, totalSeconds);
}