-8

I want to have a good understanding of the use of return *this and take of instance we have

coord& coord::operator=(const coord& other)
{
    if (this== &other)
}

My concern here is the use of this and the use of return *this

Galaxy
  • 2,363
  • 2
  • 25
  • 59

3 Answers3

0

It's quite simple:

coord& coord::operator=(const coord& other) 
{ 
   // the test on adresses below is to make sure we're not doing useless work.  
   // comparing pointers is fast.
   // making sure the addresses are different, we know we are not copying
   // the object onto itself.
   // It is both an optimization, and can, for certain memory operations, avoid bugs 
   // although modern compilers do avoid the memcpy/memmove bug, which was real pain a
   // while back.  Some CPUs _will_ puke when asked to copy and source == destination.
   if (this != &other)  
   {
       // usually member-by member copy goes here.
   }

   // returning a reference is standard for copy operations.
   // it allows for such syntaxes as:
   // coord a, b, c;
   //
   // a = b = c;
   //
   // or 
   // if ((a = b) == c) {}
   //
   // etc...
   //
   return *this;  
}

Note: this answer only refers to the question about comparing addresses and the need to return a reference. If your class requires memory allocation or copying any of the members may throw, you should check this post: [1]: What is the copy-and-swap idiom? as pointed out by Lightness Races in Orbit.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
0

this means a pointer reference to itself. Before we copy other obj, we should know whether the two objects are the same. If same, we don't need do anything.

Howo
  • 99
  • 3
0

Assuming a and b are of type coord, and a simple assignment a = b is performed, the return *this in the body of coord &coord::operator=(const coord &) returns a reference to a.

The effect of this is to permit chaining of assignments of coord objects. For example,

a = b = c;

is equivalent to (since assignment is right to left associative)

a = (b = c);

The expression b = c therefore is therefore turned by the compiler into a call of b.operator=(c), which returns a reference to b. Similarly the assignment of a is turned into a call of operator=(), so the above is equivalent to

a.operator=(b.operator=(c));

which (assuming operator=() is correctly implemented to perform a member-wise assignment) assigns both a and b to c. The reference returned by a.operator=() is, in this case, ignored by the compiler.

The if (this == &other) test is an old technique to handle self-assignment (e.g. a = a) as a special case (e.g. by doing nothing). Such a thing is generally discouraged for various reasons, such as behaving differently if the coord type has a unary operator&(). As noted by "Lightness Races in Orbit" in a comment to another answer, it is generally considered preferable to use the copy and swap idiom instead - refer for example to What is the copy-and-swap idiom?.

Peter
  • 35,646
  • 4
  • 32
  • 74