简体   繁体   English

C++ 编程:引用如何在运算符重载中工作?

[英]C++ Programming: How do references work in operator overloading?

I am trying to understand why references are used when overloading a C++ operator.我试图理解为什么在重载 C++ 运算符时使用引用。 For example, in the following piece of code:例如,在下面的一段代码中:

GenericObject& operator=(const GenericObject &rhs)
{
    if (&rhs == this)
        return *this;
    objectAttribute = GenericObject.objectAttribute;
    return *this;
}

I have three questions:我有三个问题:

  1. Why does the function return a reference?为什么函数返回一个引用?
  2. Why does the function take in a reference?为什么函数接受引用? Is it to avoid the expense of copying the contents of the object (which would be necessary if a GenericObject object was the parameter instead of a reference to a GenericObject)?是为了避免复制对象内容的费用(如果 GenericObject 对象是参数而不是对 GenericObject 的引用,这将是必要的)?
  3. Why is a reference used in the third line when comparing the right hand side to this?将右侧与此进行比较时,为什么在第三行中使用了参考?

To the best of my ability, the answers to your questions are:尽我所能,您的问题的答案是:

  1. You return a reference because it will save you later in constructor/destructor calls.您返回一个引用,因为它将在稍后的构造函数/析构函数调用中保存您。 A good rule-of-thumb a professor of mine used is "If in doubt, do as the ints do."我的一位教授使用的一个很好的经验法则是“如果有疑问,按照整数做。” When you look at an operation like int x = 1, y = 2, z = 3; x = y = z;当你看一个像int x = 1, y = 2, z = 3; x = y = z; int x = 1, y = 2, z = 3; x = y = z; , returning by reference allows you to take the value of z , assign it to y , and then take the value of (the new) y and assign it to x . ,通过引用返回允许您获取z的值,将其分配给y ,然后获取(新的) y的值并将其分配给x Returning by value requires that you create a copy of z , use that to assign y , delete the copy of z , and then create a copy of (the new) y to assign x before delete this copy.按值返回要求您创建z的副本,使用它来分配y ,删除z的副本,然后创建(新的) y的副本以在删除此副本之前分配x Might not be a big deal with ints, but it could mean a much longer program with larger data.对于整数可能不是什么大问题,但它可能意味着更长的程序和更大的数据。

  2. You are totally correct!你完全正确! For the same reasons we are trying to save time and effort in the first part, we are passing the right-hand side item by reference to avoid having to make a duplicate (which requires constructor/destructor calls) and make it const so that we cannot inadvertently change it as this is illogical with operator= .出于同样的原因,我们试图在第一部分中节省时间和精力,我们通过引用传递右侧项目以避免必须进行重复(这需要构造函数/析构函数调用)并将其设为const以便我们不能无意中更改它,因为这与operator=不合逻辑。 As a side note, extra constructor/destructor calls can be dangerous if you are not careful in some situations (see deep copies vs shallow copies for an example of this).作为旁注,如果您在某些情况下不小心,额外的构造函数/析构函数调用可能是危险的(有关此示例,请参阅深拷贝与浅拷贝)。

  3. When you make a call like x = y;当您拨打x = y;类的电话时x = y; , it is logically equivalent to x.operator=(y); , 逻辑上等价于x.operator=(y); . . Because x is some object, we need a way to refer to the entire thing, and the keyword this allows this functionality.因为 x 是某个对象,所以我们需要一种方法来引用整个事物,而关键字this允许此功能。 this is a pointer, however, so because we are trying to compare a pointer and an object, we need to point to the object held by rhs (or y in my example) and we get this address by using & .然而, this是一个指针,所以因为我们试图比较一个指针和一个对象,所以我们需要指向rhs (或在我的例子中是y )持有的对象,我们通过使用&获得这个地址。

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

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