简体   繁体   English

了解操作员

[英]Understanding Operators

Our Professor has given us a practice test with the answers to help us for our upcoming test. 我们的教授给了我们一个实践测试,并给出了答案,以帮助我们进行即将到来的测试。 I don't understand how the answer for this code is 135 . 我不明白此代码的答案是135

I understand what a class is but I am struggling how operators work. 我了解什么是课程,但我正在努力解决操作员的工作方式。 For v{6} I understand that for the object v the int v equals 13 . 对于v{6}我知道对于对象vint v等于13 I thought that in the int main that the first -v in (-v - v).print would be evaluated first in the operator that return 2 * v and that it would return 26 . 我认为在int main中, (-v - v).print中的第一个-v将首先在返回2 * v的运算符中求值,并且它将返回26 I then thought they would then be put in the last operator 然后我以为他们会放在最后一个运算符中

V operator-(int lop, const V& rop)
{
    return rop - lop;
}

but I don't think that is the case. 但我认为情况并非如此。 I have put this code into Visual Studio to mess around with it but I don't understand what is going on. 我已经将这段代码放入Visual Studio中进行处理,但是我不知道发生了什么。

#include <iostream>
using namespace std;

class V
{
    int v;

public:
    V(int a = 3, int b = 7) : v{a + b}
    {
    }

    void print()
    {
        cout << v;
    }

    V operator-(const V& rop) const
    {
        return (3 * v) + (2 * rop.v) + 3;
    }

    V operator-()
    {
        return 2 * v;
    }
};

V operator-(int lop, const V& rop)
{
    return rop - lop;
}

int main() 
{
    V v{6};
    (-v - v).print();

    return 0;  
}

Wow, this is confusing code, with re-used variable names, strange unconventional operations and such. 哇,这真是令人困惑的代码,其中包含重复使用的变量名,奇怪的非常规操作等。 The code is particularly hard to follow because V can be implicitly constructed from an integer, with 7 always added to that integer; 该代码特别难于遵循,因为V可以由一个整数隐式构造,并且始终将7附加到该整数。 even with a debugger, this took me a few moments to grok. 即使使用调试器,这也花了我一些时间。 Please never write code like this, not even for fun! 请不要写这样的代码,甚至不要为了好玩!

The result of -v in main is not an int . main-v的结果不是int It is a V object implicitly constructed from the expression 2 * v (=26), resulting in a member integer with value 33 (26+7). 这是一个V对象,由表达式2 * v (= 26)隐式构造,得到一个值为33(26 + 7)的成员整数。

It's as if you wrote: 就像您写了一样:

V operator-()
{
    return V(2 * v);
}

or, due to the default argument: 或者,由于默认参数:

V operator-()
{
    return V(2 * v, 7);
}

Then you take this new returned object and feed it to member operator-(const V&) ; 然后,将这个新的返回对象作为对象,并将其提供给成员operator-(const V&) ; same story applies. 同样的故事适用。 It produces the expression 3*33 + 2*13 + 3 , which is 128; 它产生表达式3*33 + 2*13 + 3 ,即128; again this is used to construct a new V (because that's the return type!), so add 7 to get 135. 再次使用它来构造一个新的V (因为这是返回类型!),所以加7以获得135。

operator-(int lop, const V& rop) doesn't come into it because you never performed a subtraction between an int and a V . operator-(int lop, const V& rop)不会出现在其中,因为您从未在intV之间执行减法。

The result of -v is not an int , but a V . -v的结果不是int ,而是V
Thus, the member-overloaded subtraction will be used. 因此,将使用成员重载的减法。

This is the equivalent code, without the syntactic sugar of operators, but with explicit conversion of return values: 这是等效的代码,没有运算符的语法糖,但是带有返回值的显式转换:

class V
{
public:
    V(int a = 3, int b = 7) : v{a + b}
    {
    }

    void print()
    {
        cout << v;
    }

    V subtract(const V& rop) const
    {
        return V((3 * v) + (2 * rop.v) + 3);
    }

    V negate()
    {
        return V(2 * v);
    }
private:
    int v;
};


int main() 
{
    V v{6};
    (v.negate().subtract(v)).print();

    return 0;  
}

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

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