- [Instructor] C++ provides unary operators
for incrementing and decrementing values.
This is working.cpp from chapter four of the exercise files.
Come in here and just add a variable.
We'll call it x.
And we're going to print it like this
with this increment operator on the left side
of the x here.
And when I build and run this,
you'll notice that the value
that it prints is six.
And if I duplicate this a few times,
it prints a seven and then it prints an eight.
That's because this increment operator
actually increments the value
and then returns the new value.
And it leaves that incremented value in the variable.
We can do the same thing with two minus operators.
It's actually one decrement operator.
So with the two plus signs,
it's the increment operator,
and with the two minus signs, it's the decrement operator.
And when I build and run this,
you notice we get four, three and two.
Now, what's interesting here
is if I take these operators
and put them on the other side like this,
so now this is post-increment,
the other side was pre-increment.
So now it will return the value first
and then increment it.
So the first one will print a five,
we'll build and run this
and we get five, six and seven
and of course, this same thing works
with the decrement operator.
So you have pre-fix versions and post-fix versions.
The pre-fix version operates
on the variable before it returns a value
and the post-fix version operates
on the variable after it returns the value.
Now, what's interesting with these increment
and decrement operators
is how they operate on pointers
and so I'm going to go ahead
and I'm going to delete this working copy
and I'm going to make a working copy of pointer-incr.cpp,
which is spelled like that.
Pointer-incr.cpp.
And we'll go ahead and we'll build and run this
so you can see what it does here.
I have a little function here that prints a pointer value
and what it points at.
And so the first one is printing the address
of the pointer or rather the address it's pointed to,
the value of the pointer,
which is an address
and the second is de-referencing the pointer
for a switch, printing what it points at.
And so I have an array here
of eight-bit integers.
And I make a pointer to that array
and then I increment the pointer
while printing it three times.
So I'm using the post-increment operator here.
The increment is on the right-hand side of the variable
and I'm doing it three times.
So I'm printing three values
and you'll notice that these addresses increase by one.
But if I were to change all of these uint8 to uint16,
and all the places where I have that.
I hope I caught them all,
and now I build and run, you'll notice
that this is now incrementing by two each time.
And if I change these to uint32,
and build and run,
you notice that they increment by four.
So the pointer is encoded with knowledge
of the type that it points to.
I'm declaring this pointer as a pointer
to a 32-but integer
and so when I increment that pointer,
what I want is for the pointer
to point at the next item in the array,
which is 32 bits away
or four times a byte, four bytes.
Or if it's a uint16, it's two bytes.
If it's uint8, it's one byte.
And so the pointer knows the size
of the thing that it's pointing to.
And of course, this works the same
with the pre-increment or the post-increment.
I'll build and run there
and you see the only difference is,
of course, with the pre-increment,
it's going to increment before it passes that variable
into the function
and with the post increment,
it'll increment after.
It's worth noting that pre-increment
is slightly more efficient than post increment.
And this is because post increment
actually has to make a copy of the value first
before it does the increment
and because it has to return a value
and then do the incrementing,
so it has to make a copy of the value
and then increment and then return that copy
is what it actually has to do.
And so the pre-increment
is slightly more efficient than the post-increment.
We'll cover this in some detail
in the companion course C++ Advanced Topics
as we show you how to actually implement this
in a class.
So the unary increment and decrement operators
are very useful, they're very common.
You'll see them all at the time.
Keep in mind that the prefix version increments
before returning the value
and the post-fix version increments
after returning the value.