操作符重载如何工作,为什么在我的情况下不起作用?

时间:2019-02-23 03:49:43

标签: c++ overloading operator-keyword

已为我提供了一个驱动程序函数,该函数应演示包含复杂数字的运算符重载的结果。阅读了一段时间的重载后,我设法以成功编译的方式编写了代码,但是在此过程中某个地方程序没有输出正确的值。

据我了解,重载本质上就像一个函数。传递对象,然后“函数”可以对其进行算术/任何操作,然后返回一个新对象。不过,我有点迷失的地方是重载如何知道要传递的值。例如,在我的情况下,我重载了“ +”和“ =”运算符,以便以“ x = y + z”的形式添加两个复数。当编译器遇到“ =”符号时,我假设它只是通过了左侧和右侧的所有内容并通过了这些内容?与“ +”相同。在这种情况下,它将传递“ y”,因为它是左边的对象,传递“ z”,因为它是右边的对象?

这是我当前的“复杂”类,其中包括重载定义。

class Complex {

private:
double realPart;
double imaginaryPart;

public:
// friends
friend ostream & operator<<(ostream &out, const Complex &c);
friend istream & operator>>(istream &in,  Complex &c);

// constructors
Complex()
{
    realPart = 0;
    imaginaryPart = 0;
}

Complex(double real)
{
    realPart = real;
    imaginaryPart = 0;
}

Complex(double real, double imaginary)
{
    realPart = real;
    imaginaryPart = imaginary;
}
// end of constructors

// + overloading
Complex operator+(Complex const &c)
{
    Complex Add;
    Add.realPart = realPart + c.realPart;
    Add.imaginaryPart = imaginaryPart + c.imaginaryPart;
    return Add;
}

// - overloading
Complex operator-(Complex const &c)
{
    Complex Subtract;
    Subtract.realPart = realPart - c.realPart;
    Subtract.imaginaryPart = imaginaryPart - c.imaginaryPart;
    return Subtract;
}

// * overloading
Complex operator*(Complex const &c)
{
    Complex Multiply;
    Multiply.realPart = (realPart * c.realPart) - (imaginaryPart * c.imaginaryPart);
    Multiply.imaginaryPart = (realPart * c.imaginaryPart) - (imaginaryPart * c.realPart);
    return Multiply;
}

// = overloading
Complex operator=(Complex const &c)
{
    Complex Assignment;
    Assignment.realPart = realPart;
    Assignment.imaginaryPart = imaginaryPart;
    return Assignment;
}

// == overloading
bool operator==(Complex const &c)
{
    Complex Compare;
    if (Compare.realPart == realPart && Compare.imaginaryPart == imaginaryPart)
    {
        return true;
    }
    else
    {
        return false;
    }
}

    // != overloading
bool operator!=(Complex const &c)
{
    Complex NotEqual;
    if (NotEqual.realPart == realPart && NotEqual.imaginaryPart == imaginaryPart)
    {
        return false;
    }
    else
    {
        return true;
    }
}

};

// << overloading
ostream& operator<<(ostream& out, const Complex &c)
{
    out << c.realPart;
    if (c.imaginaryPart >= 0)
    {
        out << " + " << c.imaginaryPart << "i" << endl;
    }
    else
    {
        out << " - " << fabs (c.imaginaryPart) << "i" << endl;
    }

    return out;
}

// >> overloading
istream& operator>>(istream &in, Complex &c)
{
    in >> c.realPart;
    in >> c.imaginaryPart;
    return in;
}

这是驱动程序:

int main()
{
    for (double i = 1; i < 10; ++ i)
    {
        Complex y{i * 2.7, i + 3.2};
        Complex z{i * 6, i + 8.3};

        Complex x;
        Complex k;

        std::cout << "Enter a complex number in the form: (a, b)\n? ";
        std::cin >> k; // demonstrating overloaded >>
        std::cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: " << k << '\n'; // demonstrating overloaded <<

        x = y + z; // demonstrating overloaded + and =
        std::cout << "\nx = y + z:\n" << x << " = " << y << " + " << z << '\n';
        x = y - z; // demonstrating overloaded - and =
        std::cout << "\nx = y - z:\n" << x << " = " << y << " - " << z << '\n';
        x = y * z; // demonstrating overloaded * and =
        std::cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n";

        if (x != k)
        { // demonstrating overloaded !=
            std::cout << x << " != " << k << '\n';
        }

        std::cout << '\n';
        x = k;

        if (x == k)
        {
            // demonstrating overloaded ==
            std::cout << x << " == " << k << '\n';
        }

        std::cout << std::endl;
}
}

运行时,问题似乎出在“ x”对象上。输入“ 5 2”仍将输出“ x:0 + 0i”,这使我相信问题在于“ =”的重载或流运算符。就是说,我不太清楚为什么什么也没发生。

我认为构建“ =”重载定义的方式是否存在错误,或者我可能缺少更大的东西了?

2 个答案:

答案 0 :(得分:3)

您的=是错误的;它应该返回*this

Complex& operator=(Complex const &c)
{
  realPart = c.realPart;
  imaginaryPart = c.imaginaryPart;
  return *this;
}

解决这个问题,其余大多数看起来都很理智。

答案 1 :(得分:1)

operator=()不正确:用户Yakk - Adam已经向您展示了如何修复它。使您了解为什么它是错误的以及return *this的作用;让我们看看您的原始功能:

Complex operator=(Complex const &c) {
    Complex Assignment;
    Assignment.realPart = realPart;
    Assignment.imaginaryPart = imaginaryPart;
    return Assignment;
}

在这里,您的签名使用const引用另一个Complex对象,这部分是正确的。您的返回类型是Complex对象,这在本质上是错误的,因为您不想返回对象的副本。这里的目的是执行分配。这意味着您必须更改原始的LHS实例。

在表达式A = B + C中; A被视为LHS实例。在这里,您要分配均为(B + C)值的表达式RHS

因此,当Yakk - Adam向您展示了如何通过以下方法解决此问题时:

Complex& operator=(Complex const &c) {
  realPart = c.realPart;
  imaginaryPart = c.imaginaryPart;
  return *this;
}

这里的区别之一是返回类型现在是特定对象的Reference而不是对象的副本。

另一个区别是不需要像原始版本中那样创建本地临时副本:

Complex Assignment; // this is not needed

通过将其从operator=()中删除,他只是替换了以下代码行:

// Assignment.realPart = realPart; // To
realPart = c.realPart;
// Assignment.imaginaryPart = imaginaryPart; // To
imaginaryPart = c.imaginaryPart;

在这里,您直接使用类的成员,并为他们分配属于另一个或传递给运算符的c的值。

然后最后返回具有更新后值的LHS实例;这是您必须返回解引用的this指针的地方。

*this是什么意思? this指针是一种特殊的指针,它属于所有classstruct类型。例如,任何时候您拥有类对象:

class Foo {
public:
    int bar { 10 };

    void addFive();
}; 

您可以在成员函数中直接使用this指针:

void Foo::addFive() {
    this->bar += 5; // same as below  (this) belongs to this particular instance.
    // bar += 5;        
}

关于您的operator=();由于您将在reference之前返回,因此您不能仅仅只是return this。这将返回this指针。我们不需要指向对象的指针,因为我们想要引用对象。因此我们必须通过返回this来尊重*this指针。

我希望这可以帮助您解决问题。