当它没有被定义时,它如何返回一个值? [英] How is this returning a value when it has not been defined?

查看:134
本文介绍了当它没有被定义时,它如何返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解重载操作符,我一直盯着这个时间比我想承认。我相信我理解类中的一切EXCEPT的运算符+成员。我试图教给自己与大量的信息,但我找不到任何信息,向我解释我在这里看到 - 我是一个坚定的信徒,如果我明白什么工作,然后我可以使用它更好。

I'm trying to understand overloading operators, and I've been staring at this for longer than I want to admit. I believe that I understand everything in the class EXCEPT the operator+ member. I'm trying to teach myself with the plethora of information available, but I can't find any information that explains to me what I'm seeing here--and I'm a firm believer if I understand HOW something works then I can use it better.

因此,大多数情况下,我的困惑在于编译器知道选择哪个变量temp。 (temp.x或temp.y)我意识到main()是要求c.x和c.y,但是operator +似乎是返回未定义的东西。没有三元运算符或任何可以选择返回哪个运算符。

So, mostly, my confusion lies with how the compiler knows which variable of temp to choose. (temp.x or temp.y) I realize main() is asking for c.x and c.y, but operator+ seems to be returning something that wasn't defined yet. There is no ternary operator or anything that would make it pick which one to return.

#include <iostream>
using namespace std;

class CVector {
public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
    x = a;
    y = b;
}

CVector CVector::operator+ (CVector param) {
    CVector temp;
    temp.x = x + param.x;
    temp.y = y + param.y;
    return (temp);
}

int main () {
    CVector a (3,1);
    CVector b (1,2);
    CVector c;
    c = a + b;
    cout << c.x << "," << c.y;
    return 0;
}


推荐答案


因此,大多数情况下,我的困惑在于编译器如何知道要选择哪个
变量。

So, mostly, my confusion lies with how the compiler knows which variable of temp to choose.

不明白你的意思。编译器没有选择要返回的temp变量。 temp CVector 类型的对象。它包含两个数据成员: x y 。当使用此行创建时,这些成员作为 temp 的一部分存在:

I really don't understand what you mean by this. The compiler is not choosing a variable of temp to return. temp is an object of type CVector. It contains two data members, x and y. These members exist as part of temp when it is created with this line:

CVector temp;

然后,当你这样做:

return temp;

编译器没有任何选择。它返回整个对象,其中包括一个复合对象中的 x y

There is nothing for the compiler to choose. It is returning the whole object, which includes both the x and the y in one compound object.

在您的主函数中,这行:

In your main function, this line:

c = a + b;

operator + > a 和 b 。然后将返回值( temp )( operator = )赋给 c 。因为你没有定义一个自定义赋值操作符,默认的赋值运算符只需从 temp c 。因此, temp.x 分配给 cx temp.y 分配给 cy

Calls operator+ on a and b. Then the return value (temp) is assigned (operator=) to c. Since you haven't defined a custom assignment operator, the default one kicks in, which simply does a memberwise assignment from temp to c. So, temp.x is assigned to c.x, and temp.y is assigned to c.y.

对于您的类,默认赋值运算符看起来像相同的操作语义)如果它被写出:

For your class, the default assignment operator would look like (or have identical operational semantics to) this, if it were written out:

CVector & CVector::operator=(const CVector & rhs)
{
    this->x = rhs.x;
    this->y = rhs.y;
    return *this;
}

这篇关于当它没有被定义时,它如何返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆