2
class sample
{
  private:
    int radius;
    float x,y;
  public:
    circle()
     {

     }
    circle(int rr;float xx;float yy)
     {
      radius=rr;
      x=xx;
      y=yy;
     }

 circle operator =(circle& c)
     {
      cout << endl<<"Assignment operator invoked";
      radius=c.radius;
      x=c.x;
      y=c.y;
      return circle(radius,x,y);
     }


}

int main()
{
 circle c1(10,2.5,2.5);
 circle c1,c4;
 c4=c2=c1;
}

In the overloaded '=' function the statements

radius=c.radius;
x=c.x;
y=c.y;

itself make all of c2's data members equal to c1's , so why is a return necessary? Similarly, in c1=c2+c3, c2 and c3 are added using an overloaded + operator and the value is returned to c1, but doesn't that become c1=, so shouldn't we be using another = operator to assign the sum of c2 and c3 to c1? I'm confused.

assylias
  • 321,522
  • 82
  • 660
  • 783
Nirvan
  • 147
  • 1
  • 1
  • 6
  • Related: http://stackoverflow.com/questions/2447696/overloading-assignment-operator-in-c and http://stackoverflow.com/questions/2649068/has-anyone-found-the-need-to-declare-the-return-parameter-of-a-copy-assignment-o/2649576#2649576 – CB Bailey Apr 11 '12 at 16:25

4 Answers4

6

It's not needed (i.e. a void return type is legal), but standard practice is to return a reference to *this to allow assignment chaining without any efficiency overhead. E.g.:

class circle
{
    int radius;
    float x, y;

public:
    circle()
      : radius(), x(), y()
    { }

    circle(int rr, float xx, float yy)
      : radius(rr), x(xx), y(yy)
    { }

    circle& operator =(circle const& c)
    {
        std::cout << "Copy-assignment operator invoked\n";
        radius = c.radius;
        x = c.x;
        y = c.y;
        return *this;
    }
};

int main()
{
    circle c1(10, 2.5f, 2.5f);
    circle c2, c3;
    c3 = c2 = c1;
}

Returning a new object by value, as you're doing, is certainly non-standard, as it creates unnecessary temporaries.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
3

It's not mandatory, but returning a reference to *this allows people to chain assignments, as you can with fundamental types.

However, that will only work if the assignment operator takes its argument by value or const reference; yours takes it by non-const reference, which is something you should only do in special circumstances.

circle & operator=(circle const & c) {
    radius = c.radius;
    x = c.x;
    y = c.y;
    return *this;
}

With an operator like that, c4=c2=c1 will compile, and will have the effect of assigning c1 to c2, then assigning the new value of c2 to c4.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

It's to support the idiom of a = b = c.

You're also doing it wrong; the return should be circle & not circle and the return should be return *this;.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

You can just return *this from your assignment operator function to return a reference to the current object. You can also make the value of the

circle& operator = (circle& c)
{
// do assignments
    return *this;
}
jbruni
  • 1,238
  • 8
  • 12