From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices
Increment and decrement operators - C++ Tutorial
From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices
Increment and decrement operators
- [Instructor] The unary increment and decrement operators are special cases with a few interesting twists. This is increment decrement dot cpp from chapter one of the exercise files, spelled thusly, incr-decr.cpp. And here we have a class that's just a simple wrapper class that implements the increment and decrement operator so you can see how they work. We're simply wrapping this integer value in this class. We have getValue, setValue. We have a conversion operator to convert it to an int if we need that. And then we have the four operators that we're working with here. Two increment operators and two decrement operators. And you notice that we have operator++ twice and operator-- twice. And the reason for this is that each of these increment and decrement operators has both prefix and postfix versions. So there's prefix and postfix increment and prefix and postfix decrement. So that makes four operators in total. How does the compiler tell the difference from prefix and postfix? The prefix looks just like that and the postfix has the keyword int in the parameter parentheses. And this is not because it's passed a parameter, and it's not because our class is wrapping around an integer value. The int is used in all cases and it's just there as a little hack to tell the compiler that this is the postfix version of the increment operator. And the same with the decrement operator. So here's how the actual code looks for our increment and decrement operators. It's very simple. We have a little print statement to show which operator we're using. Obviously, in production we would not include that. All we do is we increment the value and we return a reference to the object with this keyword. That's pre-increment. Post-increment is slightly more complicated, not much. First we take a copy of our object, then we increment the value and then we return the copy that we took before the increment. And this allows us to increment after the fact, after we return the value. So, not actually incrementing after the return coz obviously we can't do that so what we do is we save this copy, we do the increment and then we return the saved copy that we had saved before the increment. Same thing in the decrement. Exactly the same code except we're decrementing instead of incrementing. We have a little formatter specialization. You'll notice that we use our getValue member function. And then here in main, we print a value, we increment it, and then we print a value. And so, I'm going to go ahead and run this. And you'll see that it says the value's 42. We initialized our object with the value 42, and then we increment and we see it says pre-increment value's 43. That's because we put the ++ in the prefix position before the object. And then you notice that afterwards the value is still 43. If I move this ++ to the postfix position and run this again, you see that post-increment, our value is 42. And then the next time we look at the object, it is 43. And the same is true, obviously for decrement. We have the prefix decrement where it decrements right away, and we have the postfix decrement where it decrements effectively after the statement. It's worth understanding the increment and decrement operators, even if it's just to know that the postfix versions are a bit more expensive than the prefix versions. And this is why many of us prefer the prefix operators in cases where it otherwise doesn't matter.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.