Prefix/Postfix increment operators?

This is a correct implementation. It is typical that a postfix operator will be worse on performance because you have to create another copy before doing the increment (and this is why I've gotten in the habit of always using prefix unless I need something else).

This is a correct implementation. It is typical that a postfix operator will be worse on performance because you have to create another copy before doing the increment (and this is why I've gotten in the habit of always using prefix unless I need something else). With return-by-reference, you're returning an l-value reference to the current object.

The compiler would typically implement this by returning the address of the current object. This means that returning the object is as simple as returning a number. However, with return-by-value, a copy must be done.

This means there's more information to copy over during the return (instead of just an address) as well as a copy constructor to call. This is where your performance hit comes in. The efficiency of your implementation looks on-par with typical implementations.

EDIT: With regards to your addendum, no, they are not aliases. You have created two separate objects. When you return by value (and when you created a new object from within the postfix increment operator) this new object is placed in a distinct memory location.

However, in the following code, a and be are aliases: int a = 0; int& be = ++a; be is an address which references a.

Perfect - thanks. – Cam Jul 5 '10 at 17:45 1 Correct in general, modulo possible Return Value Optimization (en.wikipedia. Org/wiki/Return_value_optimization).

– Nikolai N Fetissov Jul 5 '10 at 17:54.

It is more idiomatic to call the prefix increment of the object itself in the postfix increment: X operator++(int) { X copy(*this); operator++(); // or alternatively, ++(*this); return copy; } The logic of incrementing an X object is thus contained in the prefix version.

Yep, this frees me from posting the same correction. +1 from me. – sbi Jul 5 '10 at 18:16.

Your operators are implemented correctly. In the prefix operator, no copies of X are made. In the postfix operator, one copy is made for ret, and potentially another copy is made when returning from the function, but all compilers will elide this copy.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions