-1
#include <stdio.h>
class R
{
    public:
    int k, depth;
    R() :k(0), depth(0) {}

    const R&  operator=( const R & r )
    {
        if( this != &r )
        { 
            k = r.k; 
            depth = r.depth+1; 
        }
        return r;
    }
};

int main()
{
    R r1, r2, r3;
    r1 = r2 = r3;
    printf( "%d %d %d\n",
           r1.depth, r2.depth, r3.depth );
}

I don't get what the method const R& operator=( const R & r ) is doing. From it's signature to what it is doing. Is this operator overloading? What is it trying to do? What is the output of this? I admit that I haven't done C++ in a while.

rexbelia
  • 149
  • 2
  • 4
  • 12
  • 1
    That's explained in any halfway-decent C++ book, but you guessed right, it's an overloaded operator. – Ulrich Eckhardt Jan 13 '15 at 22:45
  • 1
    Its also thoroughly explain on this site:[`[cpp] operator overloading`](http://stackoverflow.com/questions/4421706/operator-overloading) in the search box and read the #1 ranked result. – WhozCraig Jan 13 '15 at 22:55

3 Answers3

2

I don't get what the method const R& operator=( const R & r ) is doing.

It gets called when you assign one R to another, like

r1 = r2 = r3;

In the function, this corresponds to the LHS of the operator and the r corresponds to the RHS of the operator.

From it's signature to what it is doing.

It defines an overloaded operator function for the assignment operator, operator= whose LHS is an object of type R and whose RHS is an object of type const R&.

Is this operator overloading?

Yes, it is operator overloading.

What is it trying to do?

If the LHS and RHS point to different objects (that is the check if (this != &rhs)), it sets the member variables corresponding to this, the LHS of the operator, from the values of the corresponding member variables from r, the RHS of the operator. Then, it returns a reference to the RHS, which is not a good thing but that's what it does.

A better implementation would be:

R& operator=( const R & r )
{
    if( this != &r )
    { 
        k = r.k; 
        depth = r.depth+1; 
    }
    return *this;
}

which returns a reference to the object on the LHS of the operator.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Why is it bad to return a reference to the RHS of the operator? Can you elaborate on this? So the output would be "3,2,1"? – rexbelia Jan 13 '15 at 23:04
  • @rexbelia, it doesn't follow standard C++ convention for primitive types. When you use `(c = getchar())`, the expression evaluates to `c`. When you use `r1 = r2`, the expression evaluate to `r1`, not `r2`. – R Sahu Jan 13 '15 at 23:07
  • Or perhaps "0,0,0" because it returning the RHS of the operator each time. – rexbelia Jan 13 '15 at 23:07
  • The way the code is written, the output should be "1 1 0". – R Sahu Jan 13 '15 at 23:14
  • Why doesn't it get incremented the second time? As in "2 1 0"? Also according to my it is copying over r's member variables into this. But it doesn't return this. So each time it is returning itself (what was the original r), so wouldn't the output be "0 0 0"? – rexbelia Jan 13 '15 at 23:31
  • `r1 = r2 = r3;` is equivalent to `r1 = (r2 = r3);`. The way it's been implemented, that is equivalent to `r2 = r3;` followed by `r1 = r3;`. – R Sahu Jan 13 '15 at 23:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68777/discussion-between-rexbelia-and-r-sahu). – rexbelia Jan 13 '15 at 23:38
0

It is an operator overload. It allows this:

r1 = r2 = r3

With each assignment, your code is adding 1 to depth so it knows how many times it has assigned R.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
0

It's the copy assignment operator (=). It copies the values of the right-hand instance to the left-hand instances (and increments one, which is strange).

D Stanley
  • 149,601
  • 11
  • 178
  • 240