Reputation: 258
I have a question about my C++ homework. I am just confused about *this.
The code below is what I have.
My question is why the condition in the if statement in the = operator is true?
#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;
}
Upvotes: 0
Views: 193
Reputation:
It is comparing the address of the object passed to the class with address object that is pointed by this
. If the addresses are the same then you have passed the object itself which we don't want to operate upon. With *this
you are in fact passing the pointer reference in the operator.
Upvotes: 0
Reputation: 596352
The if
statement evaluates to true
because you are not assigning an object instance to itself, thus satisfying the !=
operator. You have two separate objects, x
and y
. They have their own unique this
values. 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.
Upvotes: 0
Reputation: 1133
*this == &c
only in case of the left operand of operator= is the same as the right operand.
In case of x = y
==> *this != &c
.
In case of x = x
==> *this == &c
because both are the same.
It is commonly use because you don't need a copy if you assign to yourself.
Upvotes: 1
Reputation: 372814
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
abc a;
a = a;
Then you will call operator =
on a
, passing itself as a parameter. 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. 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. 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. Returning *this
by reference means that you can write things like
(a = b) = c;
Which, while silly, is supposed to be legal C++.
Hope this helps!
Upvotes: 0
Reputation: 394051
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. 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
.
Upvotes: 5