简体   繁体   English

在这种情况下,运算符重载如何工作?

[英]How does operator overloading work in this case?

I get how this operator overloading works here in this code..... 我在这段代码中得到了这个运算符重载的工作原理.....

class operatorOver {
public:
    int a, b, c;
};

operatorOver operator+(const operatorOver &a, const operatorOver &b) {
    operatorOver aa;
    aa.a = b.a + a.a;
    return aa;
}

int main()
{
    operatorOver aaa, bbb, ccc;

    aaa.a = 100;
    bbb.a = 200;
    ccc = aaa + bbb;

    cout << ccc.a << endl;

    system("pause");
};

but this version I don't understand how this one works here.... 但这个版本我不明白这个如何在这里工作....

class operatorOver {
public:
    operatorOver operator+(const operatorOver &a) {
        operatorOver aa;
        aa.a = (*this).a + a.a;
        return aa;
    }

    int a, b, c;
};

int main()
{
    operatorOver aaa, bbb, ccc;

    aaa.a = 100;
    bbb.a = 200;
    ccc = aaa + bbb;

    cout << ccc.a << endl;

    system("pause");
};

the 1st one I showed, I'm assuming the code of the operator overloading here is taking into 2 object classes in order for it to work... 我展示的第一个,我假设运算符重载的代码是进入2个对象类,以便它工作...

but how come the 2nd example it's showing that I don't need to create another object class in it's parameters but still work... when you look in main() you see that there are 2 object classes still being passed in.... I'm lost. 但是为什么第二个例子显示我不需要在它的参数中创建另一个对象类但仍然有效...当你在main()中查看时,你会看到仍有2个对象类被传入... 。 我迷路了。

In the second example, two object are passed. 在第二个示例中,传递了两个对象。 There's a and there is also this . a ,也有this The object passed as this is the left side of the operation. 作为传递的对象this是操作的左侧。

Also note that your member operator+ should be const, since it doesn't mutate any data members of this . 另请注意,您的成员operator+应该是const,因为它不会改变this成员的任何数据成员。

Your member operator also invoke undefined behavior, since you are using a unassigned value: 您的成员运算符也会调用未定义的行为,因为您使用的是未分配的值:

// The member function
operatorOver operator+(const operatorOver &a) {
    operatorOver aa;

    // What is the value of aa.a? Undefined behavior!
    aa.a = aa.a + a.a;
    return aa;
}

To be equivalent to you non-member function, it should be this implementation: 为了等同于你的非成员函数,它应该是这个实现:

// The member function
operatorOver operator+(const operatorOver &a) const {
    operatorOver aa;

    // Member `a`, could be written `this->a`
    aa.a = a + a.a;
    return aa;
}

Some of the binary operators, such as + , can be overloaded as member functions as well as non-member functions. 一些二元运算符(例如+ )可以作为成员函数和非成员函数重载。

When + is overloaded as a member function, the function needs to be declared with one argument. +作为成员函数重载时,需要使用一个参数声明该函数。 When the operator is used as: 当运算符用作:

a + b

the call is resolved as 呼叫被解决为

a.operator+(b);

When it is overloaded as a non-member function, the function needs to be declared with two arguments. 当它作为非成员函数重载时,需要使用两个参数声明该函数。 When the operator is used as: 当运算符用作:

a + b

the call is resolved as 呼叫被解决为

operator+(a, b);

Further reading: http://en.cppreference.com/w/cpp/language/operators 进一步阅读: http//en.cppreference.com/w/cpp/language/operators

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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