1

Hello I have a class Truck with only one property of type int. I am not using any pointers in the whole class. I have written 2 versions of the operator=:

 Truck& operator=( Truck &x)
  {
     if( this != &x)
     {           
         price=x.getPrice();
     }
     return *this;
  }

 Truck operator=(Truck x)
  {
     if( this != &x)
     {            
         price=x.getPrice();            
     }
     return *this;
  }

Both of them work, but is there any performance issue with anyone of them? And, what if I used pointers to declare my properties, should I stick to the first type of declaration?

cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • Do you need the self-assignment check? – Chris Drew May 25 '17 at 21:16
  • Reference return costs almost nothing. Object copy return is possibly expensive. It is evident to all, isn't it? – ilotXXI May 25 '17 at 21:27
  • 1
    Self assignment check is useless in `Truck operator=(Truck x)` because `x` is an Automatic copy and cannot have the same address as `this`. But it does remind me of some helpful reading: [What is the copy-and-swap idiom?](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – user4581301 May 25 '17 at 21:29
  • 3
    *Both of them work, but is there any performance issue with anyone of them?* -- The performance issue is you trying to tell the compiler that you know better. When you write your own assignment operator, and in your case, for really no reason since all you have is an `int`, you are thwarting any optimizations that the compiler could or would have done if it had the default assignment operator to deal with. Bottom line -- let the compiler do its job -- more times than not, it will do the job better than you. – PaulMcKenzie May 25 '17 at 21:55

2 Answers2

1

Both of them work, but is there any performance issue with anyone of them?

There is a potential performance issue with both of the code samples you've posted.

Since your class only has an int member, writing a user-defined assignment operator, regardless of how well-written it may look, could be slower than what the compiler default version would have achieved.

If your class does not require you to write a user-defined assignment operator (or copy constructor), then it is more wise not to write these functions yourself, as compilers these days know intrinsically how to optimize the routines they themselves generate.

The same thing with the destructor -- that seemingly harmless empty destructor that you see written almost as a kneejerk reaction can have an impact on performance, since again, it overrides the compiler's default destructor, which is optimized to do whatever it needs to do.

So the bottom line is leave the compiler alone when it comes to these functions. If the compiler default versions of the copy / assignment functions are adequate, don't interfere by writing your own versions. There is a potential for writing the wrong things (such as leaving out members you could have failed to copy) or doing things less efficient than what the compiler would have produced.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

Way 1 is a valid way for assign operator, except it is recommended to pass a constant reference there. It returns a reference to this, i.e. a lightweight pointer.

Way 2 can decrease performance. It constructs and returns a copy of this object. Furthermore, it is invalid. Why a reference return in assign operator is a standard signature? It allows expressions like

copy1 = copy2 = original;
while ((one = two).condition())
    doSomething();

Let's consider the following:

(copy = original).changeObject();

With way 1 this expression is what a programmer expect. In the second way it is incorrect: you call changeObject for a temporary object returned by the assign operator, not for a copy.

You can say: "I don't want to use such ugly syntax". In this case just don't allow it and return nothing in operator=. Hence, it is recommended to return a reference to this.

See also links in comments, they seem to be useful.

ilotXXI
  • 1,075
  • 7
  • 9