Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Assignment operator

Posted on 2003-03-01
6
Medium Priority
?
183 Views
Last Modified: 2010-04-01
When at University I was told that to write an overloaded assignment operator I must return a reference to the type of object being assigned, eg

class one
{
public:
     one(int aVal, int bVal, int cVal) : a(aVal), b(bVal), c(cVal) {}
     one& operator = (const one& rhs) {
          this->a = rhs.a;
          return *this;
     }    
     protected:
     int a;
     int b;
     int c;
};

int main () {
     one first(1,2,3);
     cout << first << endl;
     one second(4,5,6);
     first = second;                         // Equivalent to 'first.operator =(second);'
     cout << first << endl;
}

Now this gives the expected output of
___________________________
The value of a is 1

The value of a is 4.
___________________________

But I do not see why I need to return a reference as all necessary assignment is done with 'this->a = rhs.a;'

Indeed I rewrote the assignment operator to

     void operator = (const one& rhs) {
               this->a = rhs.a;
     }    

and the result is still the same !

so what were my lecturers on about with this reference being returned with *this ?

Cheers in advance.
0
Comment
Question by:mrwad99
  • 3
  • 2
6 Comments
 

Expert Comment

by:pku_hawk
ID: 8048137
just because you maybe use like this
a=b=c;
it's work like this
a=(b=c);
because b=c return the same as c,so after the continue assignment a also equals c
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8051066
also for the case a = a ...
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 80 total points
ID: 8051073
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:mrwad99
ID: 8051427
Cheers,

So in general I should always be returning *this then ?

0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8051500
some claim it's not worth it to leave it out, so probably yes.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8051508
Thanks to all on this one.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question