Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assignment operator overloading in c++

I have used the following code for assignment operator overloading:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs) {      if(this == &rhs)         return *this;      itsRadius = rhs.getRadius();      return *this; } 

My Copy Constructor is this:

SimpleCircle::SimpleCircle(const SimpleCircle & rhs) {     itsRadius = rhs.getRadius(); } 

In the above operator overloading code, copy constructor is called as there is a new object is being created; hence I used the below code:

SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs) {     if(this == &rhs)        return *this;     itsRadius = rhs.getRadius();     return *this; } 

Its working perfectly and the copy constructor problem is avoided, but is there any unknown issues (to me) regarding this ?

like image 237
kaushik Avatar asked Apr 09 '12 16:04

kaushik


People also ask

What is assignment operator overloading?

Assignment Operators Overloading in C++You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.

How do you write an assignment overloaded operator?

Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we'll get to. The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.

How do you define assignment operator?

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.


2 Answers

There are no problems with the second version of the assignment operator. In fact, that is the standard way for an assignment operator.

Edit: Note that I am referring to the return type of the assignment operator, not to the implementation itself. As has been pointed out in comments, the implementation itself is another issue. See here.

like image 100
juanchopanza Avatar answered Oct 04 '22 09:10

juanchopanza


The second is pretty standard. You often prefer to return a reference from an assignment operator so that statements like a = b = c; resolve as expected. I can't think of any cases where I would want to return a copy from assignment.

One thing to note is that if you aren't needing a deep copy it's sometimes considered best to use the implicit copy constructor and assignment operator generated by the compiler than roll your own. Really up to you though ...

Edit:

Here's some basic calls:

SimpleCircle x; // default constructor SimpleCircle y(x); // copy constructor x = y; // assignment operator 

Now say we had the first version of your assignment operator:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs) {      if(this == &rhs)         return *this; // calls copy constructor SimpleCircle(*this)      itsRadius = rhs.getRadius(); // copy member      return *this; // calls copy constructor } 

It calls the copy constructor and passes a reference to this in order to construct the copy to be returned. Now in the second example we avoid the copy by just returning a reference to this

SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs) {     if(this == &rhs)        return *this; // return reference to this (no copy)     itsRadius = rhs.getRadius(); // copy member     return *this; // return reference to this (no copy) } 
like image 45
AJG85 Avatar answered Oct 04 '22 09:10

AJG85