Hello again!
In this video, we are going to look at prefix and postfix operators.
C++ has two versions of the increment operator.
The prefix version has the "++" before the argument, the object that is going to be incremented.
This will add one to the argument and return the new value. For the postfix version,
the "++" comes after the object, and this will add one to it and then return the original object.
So this will make a copy of the object, before it modifies it, and then returns the copy of the original
object.
This was actually a single processor instruction, on the computer on which C was developed, and the inventors
of C thought it might be useful to have this operator in their language.
They are also prefix and postfix decrement operators which work the same way, except they subtract
one instead of adding one.
If we have a pointer to an element of an array, then the "++" operator will increment the pointer,
and that will move it to the next element.
So if we have a pointer to the first element of an array, and we increment it, then the pointer
will now be pointing to the second element in the array.
And something that we have all done.
We can use this for iterating over the elements of an array. So we get a pointer to the first element.
If we use the name of an array where a pointer is expected, we get a pointer to the first element.
And then we can iterate over the elements of the array. We can dereference this pointer to get the current
element, and then do something with it, and then we increment the pointer at the end of the loop, to
go on to the next element.
And we can write similar code with iterators and container elements. So we can increment iterators to step through
the elements of the container.
So here we are writing the dereference and the increment as two separate statements.
With the postfix operator,
we can write it all in one single statement, like this.
So star,
"p", ++.
The increment has a higher operator precedence than the dereference.
So this will increment "p", return the original value and then dereference this original value.
So if "p" is pointing to the first element, this will move "p" to the second element, but it will
dereference the first element.
If you are writing a for loop, and you use the increment operator to increase the loop counter,
then you can use either version of the operator.
I remember actually, when I was learning C, many years ago, I read a book which said that the prefix
operator increment at the start of the loop and the postfix operator increments at the end of the
loop.
Well, I found out years later, that is complete rubbish!
It is always the end of the loop, regardless of which one you use.
But there is an important difference, though. With the postfix version, that makes a copy of the
variable, before incrementing it.
So in your loop you would have code like this at the end. So it is going to back up the old counter and then
increment the counter, and then return the old value of the counter.
So it is beter to use the prefix version for loop counters.
Admittedly, modern compilers are very clever.
They will probably see that the temporary variable is never used, and optimize it away, but it is still
better to use the prefix version.
If we are writing a class that has arithmetical operators, or something that works like a pointer, if
we are writing an iterator, for example, we should probably implement these operators. For the prefix
version,
we just do whatever we need to do to increment the members.
Then we return the modified object, and we can do that by reference.
And for the prefix decrement operator,
that works the same way as well.
For the postfix operator.
We need to make a copy of the object before we modify it.
Then we do the increment, and then we return the original object.
So we are returning a local variable.
So we do not want to do that by reference.
That is a very bad idea.
So we return by value.
So we have two functions called "operator plus
plus". One returns by reference and one returns by value.
But in C++, you cannot overload on return type.
So we need some way to tell the compiler, which is the prefix version and which is the postfix.
And the rule is that if you put an int argument, then that is the postfix version.
And if you have no arguments, that is the prefix version.
So we put an int arguments here, but you do not need to use that.
So let's try that out with our complex number
class. We have our real and imaginary members.
We have the constructor. And then we have our operators for incrementing and decrementing. The prefix version will return
by reference.
The postfix version returns by value, and takes an int argument.
And then we have a member function to printout the members. For the prefix operator,
we are adding one to the object.
One is a real number.
So we just add one to the real part, and we leave the imaginary part unchanged.
And then we return the modified object.
For the postfix operator,
we copy the object before modifying, and then we return this copy.
And the same for the decrement operators.
In the main function, we create a complex object.
Then we print out its members.
We call the prefix operator.
Then we call the postfix operator.
This should return the original object.
So we are going to check that.
We are going to use that to initialize a new object.
And then we do the same for the decrement operators.
So let's see what happens.
So there is our original object. When we increment it, the real part goes up to 6, from 5, and the imaginary
part is unchanged.
When we call the postfix version, the real part goes from 6 to 7, but the returned object still has
a real part of 6.
And similarly for the decrement. The postfix operator takes the real part from 6 to 5, but the
returned object still has a member with the value 6.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!