Assignment operator

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.
LVL 19
mrwad99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pku_hawkCommented:
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
n_fortynineCommented:
also for the case a = a ...
0
n_fortynineCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

mrwad99Author Commented:
Cheers,

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

0
n_fortynineCommented:
some claim it's not worth it to leave it out, so probably yes.
0
mrwad99Author Commented:
Thanks to all on this one.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.