简体   繁体   English

这在 c++ 中。它是如何工作的?

[英]this in c++. how does it work?

I have a question about my C++ homework.我对我的 C++ 作业有疑问。 I am just confused about *this.我只是对 *this 感到困惑。

The code below is what I have.下面的代码是我所拥有的。

My question is why the condition in the if statement in the = operator is true?我的问题是为什么 = 运算符中的 if 语句中的条件为真?

#include <cstring>
class abc {
      char p[9];
      int inc;
   public:
      abc( ) { inc = 8; strcpy(p, "10010101"); }
      ~abc( );
      abc& operator=(const abc &);

};

abc::~abc( ) {

}
abc& abc::operator=(const abc &c) {
   if(this != &c) { //my question is why this condition is true?
      inc = c.inc - 2;
      for(int i=0; i<inc; i++) {
     p[i] = c.p[i] + 2;
      }
   }
   return *this;
}

int main( ) {
   abc x, y;
   x = y;
   return 0;

}

because you don't want to make a copy if you are assigning to yourself, that is the reason for the if condition, *this returns self instance.因为如果你分配给自己,你不想制作副本,这就是if条件的原因, *this返回 self 实例。 It is true because you are trying to assign y to x and as they are both different instances you then make a copy of y to x .这是真的,因为您试图将y to x并且由于它们都是不同的实例,因此您将 y copy y to x

*this == &c only in case of the left operand of operator= is the same as the right operand. *this == &c仅在 operator= 的左操作数与右操作数相同的情况下。
In case of x = y ==> *this != &c .x = y ==> *this != &c的情况下。
In case of x = x ==> *this == &c because both are the same.x = x ==> *this == &c的情况下,因为两者相同。 It is commonly use because you don't need a copy if you assign to yourself.它很常用,因为如果您分配给自己,则不需要副本。

The condition条件

if(this != &c) { //my question is why this condition is true?

is used to detect self-assignment.用于检测自赋值。 If you have an object of type abc and write如果你有一个 object 类型的abc并写

abc a;
a = a;

Then you will call operator = on a , passing itself as a parameter.然后您将调用operator = on a ,将其自身作为参数传递。 Many implementations of the assignment operator break in this case, because in cleaning up the object in preparation for the copy, the argument to operator = (in this case, a itself) will also be cleaned up.在这种情况下,许多赋值运算符的实现都会中断,因为在清理 object 以准备复制时, operator =的参数(在本例中a本身)也将被清理。 It can easily lead to crashes.它很容易导致崩溃。 Consequently, many implementations of operator = begin with a test that the argument and the receiver object aren't the same object. The above test does this by seeing if the this pointer (the receiver) and the address of the argument ( &c ) are the same.因此,许多operator =的实现都是从测试参数 object 和接收者 object 不同开始的。上面的测试是通过查看this指针(接收者)和参数的地址( &c )是否相同来实现的。相同。 If so, the objects are the same and the assignment shouldn't be done.如果是这样,对象是相同的并且不应该完成分配。 Otherwise, the objects aren't the same and the assignment should be done.否则,对象不相同,应该完成分配。

As for returning *this , since this is a pointer to the receiver object, *this is the receiver object itself.至于返回*this ,因为this是指向接收者 object 的指针, *this是接收者 object 本身。 Returning *this by reference means that you can write things like通过引用返回*this意味着你可以这样写

(a = b) = c;

Which, while silly, is supposed to be legal C++. C++ 虽然愚蠢,但应该是合法的。

Hope this helps!希望这可以帮助!

The if statement evaluates to true because you are not assigning an object instance to itself, thus satisfying the != operator. if语句的计算结果为true ,因为您没有将 object 实例分配给自身,因此满足!=运算符。 You have two separate objects, x and y .您有两个单独的对象xy They have their own unique this values.他们有自己独特this价值观。 Had you said x = x instead in main() , then the != operator in the if statement would evaluate to false instead because the two values would match.如果您在main()中改为使用x = x ,那么if语句中的!=运算符将评估为false ,因为这两个值将匹配。

It is comparing the address of the object passed to the class with address object that is pointed by this .它将传递给 class 的 object 的地址与this指向的地址 object 进行比较。 If the addresses are the same then you have passed the object itself which we don't want to operate upon.如果地址相同,那么您已经传递了我们不想对其进行操作的 object 本身。 With *this you are in fact passing the pointer reference in the operator.使用*this实际上是在运算符中传递指针引用。

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

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