Operator Overload Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 2
Reputation: hmaich is an unknown quantity at this point 
Solved Threads: 0
hmaich hmaich is offline Offline
Newbie Poster

Operator Overload Question

 
0
  #1
May 10th, 2008
Hello all,

I have a doubt concerning to overloaded operators.
Does the compiler create new temporary object to the elements involved on the overloaded operation?

E.g. Operator + overloaded.

a = b + c;

Does the compiler creat temporary object for b and c?

See the following code and the print outs. When the + operator is called, 3 new objects are destroyed and I cant see the construction message.
I guess the default copy constructor is called by the compiler, but I am not sure.

Does any one knows the answer for this question???

Thank you,
Helbert

  1. // Class1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8. class Rectangle{
  9. int x, y;
  10.  
  11. public:
  12. Rectangle(int x, int y);
  13. Rectangle(){cout << "Created: " << this << endl;};
  14. void set_values(int x, int y);
  15. int getX() {return x;};
  16. int getY() {return y;};
  17. int area() {return (x*y); }
  18. Rectangle operator +(Rectangle);
  19. ~Rectangle();
  20. };
  21.  
  22. Rectangle::Rectangle(int x, int y){
  23. this->x = x;
  24. this->y = y;
  25. cout << "Created: " << this << endl;
  26. }
  27.  
  28. Rectangle::~Rectangle(){
  29. cout << "Retangle has been destroyed " << this << endl;
  30. }
  31.  
  32. Rectangle Rectangle::operator +(Rectangle add){
  33. Rectangle tmp;
  34. tmp.set_values(add.getX(), add.getY());
  35. return tmp;
  36. }
  37. void Rectangle::set_values(int x, int y){
  38. this->x = x;
  39. this->y = y;
  40. }
  41. int main(){
  42.  
  43. Rectangle rect(2,3);
  44. Rectangle rectb(1,2);
  45. Rectangle rectc;
  46.  
  47. cout << "AREA: " << rect.area() << endl;
  48.  
  49. cout << "AREA: " << rectb.area() << endl;
  50.  
  51. rectc = rect + rectb;
  52.  
  53. cout << "AREA: " << rectc.area() << endl;
  54. return 0;
  55. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Operator Overload Question

 
0
  #2
May 10th, 2008
with
  1. class Rectangle
  2. {
  3. //...
  4. public:
  5. //...
  6. Rectangle operator +(Rectangle);
  7. //...
  8. };

// a, b, c are Rectangles
a = b + c ; evaluates to a = b.operator+(c) ; since c is passed by value, a copy of c is made (and destroyed later).
the return value is an anonymous temporary, which is constructed on return (and destroyed).
if you got a third construct/destroy, the (perhaps old?) compiler you are using is not doing any NRV optimization (in the implementation, temporary Rectangle tmp is constructed and destroyed on return).

to avoid the copy, and to make the code const-correct, write
class Rectangle
{
  //...
  public:
  //...
     Rectangle operator+ ( const Rectangle& ) const ;
  //...
};

note: adding rectangles does not seem to be intuitive.
Last edited by vijayan121; May 10th, 2008 at 11:54 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: hmaich is an unknown quantity at this point 
Solved Threads: 0
hmaich hmaich is offline Offline
Newbie Poster

Re: Operator Overload Question

 
0
  #3
May 11th, 2008
Thanks for the explanation.

About the solution ( const Rectangle& ) const ; , if I do so, I get a compiling error ": error C2662: 'Rectangle::getX' : cannot convert 'this' pointer from 'const Rectangle' to 'Rectangle &' Conversion loses qualifiers
. Any clue??

About the "adding rectangles" stuff, it is just an exercice. I started with the rectangles and then adding functionalities :-)

Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Operator Overload Question

 
0
  #4
May 11th, 2008
You can't overload the + operator to be const unless you change the operation to not modify *this. In other words, if you change *this, get rid of the second const.

The + operator typically is expected to not modify the b object (as vijayan121 explained with a = b.operator+(c) ;).

Hence, the typical layout of such a function is:
  1. Rectangle Rectangle::operator + (const Rectangle& rect) const
  2. {
  3. Rectangle result;
  4. result.x = this->x + rect.x;
  5. ...
  6. return result;
  7. }

Unless your object uses a lot of memory or other resources or has existential side-effects, I wouldn't really worry too much about temporaries. For something like your Rectangle, the difference is negligible: copying is fast and often optimized away.

Hope this helps.

[edit]
Oh yeah, often, to avoid duplicating code, the + operator is often written in terms of the += operator:
  1. Rectangle Rectangle::operator + (const Rectangle& rect) const
  2. {
  3. return (Rectangle( *this ) += rect);
  4. }
Last edited by Duoas; May 11th, 2008 at 10:47 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 530 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC