?
Solved

Assignment operator

Posted on 2003-03-01
6
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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