使用重载后增量运算符内的变量结果重载preincrement运算符

时间:2017-02-13 03:16:16

标签: c++ operator-overloading runtime-error pre-increment

我有一个重载的preincrement运算符,它应该采用像(3 + 6i)这样的复数... ....在这种情况下,a,并通过一个方形函数来运行它进入(-27 + 36i)。我也有一个postincrement运算符,它做同样的事情。 postincrement运算符工作正常,当我在main中使用它时,我得到你所期望的(-27 + 36i)没问题。

然而,当我尝试在主要内部使用preincrement运算符时,(我期待相同的答案:( - 27 + 36i))而不是我给出的平方( - 27 + 36i)这是(-567-1944i)。我不知道为什么会这样,或者我怎么纠正它。我已经检查了序列问题,并且我已经能够辨别出任何内容。我应该使用post和pre increment运算符来执行代码体内的square函数。这是代码:

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

class ComplexNum
{
public:
    ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard
    ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
    ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
    ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
    ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators

    //overloaded operators
    ComplexNum& operator =  (const ComplexNum& that) = default;
    ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); }
    ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); }
    ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); }
    ComplexNum& operator ++() { return square(*this); } //called for ++num
    ComplexNum operator ++(int) { return square(*this); } //called for num++

    ostream& print(ostream& stm = cout) const;

private:
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)

    //non-member overloaded operators
    //a is passed by value
    friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; }
    friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; }
    friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; }
    //friend ComplexNum operator++ (const ComplexNum& a) { return ++a; } //friend for ++num
    //friend ComplexNum& operator++ (const ComplexNum& a, int) { return a++; } //friend for num++

    friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); }
};

ComplexNum::ComplexNum(float a, float b)
{
    real = a;
    imaginary = b;
}

ComplexNum& ComplexNum::getComplexNum()
{
    ComplexNum keyboard;
    cout << "Enter real part of complex number: ";
    cin >> real;

    cout << "Enter imaginary part of complex number: ";
    cin >> imaginary;

    return keyboard; 
}

ComplexNum& ComplexNum::square(ComplexNum a)
{
    this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
    this->imaginary = (2 * (a.real * a.imaginary));
    return *this;
}

ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
    this->real = a.real + b.real;
    this->imaginary = a.imaginary + b.imaginary;
    return *this;
}

ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
    this->real = a.real - b.real;
    this->imaginary = a.imaginary - b.imaginary;
    return *this;
}

ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
    this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
    this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
    return *this;
}

ostream& ComplexNum::print(ostream& stm) const
{
    return stm << "(" << noshowpos << real << showpos << imaginary << "i)";
}

int main()
{
    ComplexNum a, b;
    cout << "First Complex Number:" << endl;
    a.getComplexNum();
    cout << endl;
    cout << "Second Complex Number:" << endl;
    b.getComplexNum();
    cout << endl;
    cout << fixed << setprecision(2)
        << "a == " << a << '\n'
        << "b == " << b << '\n'
        << "a+b == " << a + b << '\n'
        << "a-b == " << a - b << '\n'
        << "a*b == " << a*b << '\n'
        << "a*a == " << a*a << '\n'
        << "b*b == " << b*b << '\n';
    cout << "a*a (using postincrement) == " << a++ << '\n'; //works fine
    cout << "a*a (using preincrement) == " << ++a << '\n'; //squares the square, instead of giving me the same answer as a++ 
        cout << endl;

    system("PAUSE");
}

1 个答案:

答案 0 :(得分:1)

你的postincrement运算符正在改变a的值,所以当你的preincrement运算符被调用时,a已经被平方(所以它再次被平方)。你的postincrement有同样的问题;交换你会看到的那两条输出线。

调用a++的结果是a.square(a);。这将创建a的副本,并将广场存储回a。然后返回对修改后的a的引用作为增量值。

通常,preincrement操作符可以返回引用,因为新值在现有对象中,但是postincrement应该返回一个新对象而不是修改现有对象。