Overloading Operators in C++!
Hey guys! It’s the end of the week. Which means, another tutorial! Last couple weeks, we explored the Monte Carlos method, but this time, let’s change it up a little bit. Instead, we’ll be looking at some C++ operators, and more specifically, overloading them.
- 'this' pointer
What are overloading operators? Well take this example:
We get an error! Why? Well adding 5 to an object? What does that mean? But how do we add a number to
value? Well the attribute is private, so our only choose are methods.
Well, that's good an all, but this is C++! There has to be a better way. Overloading operators.
Don't worry about the syntax for now. Just know that overloading operators change the behavior of normal operators. Now, the compiler doesn't see it as adding
5 to the object, but rather adding
Ready yourself to trash all your getters and setters.
Let's start off by taking a look at the previous example.
Notice how the method and overloaded operator are very similar. An overloaded operator starts off with it's return type (it's usually
bool), then followed by the keyword
operator, then the actual operator itself, and a parameter if required. Then inside the overloaded operator function, just do what you would normally do.
Here's another example.
This time we overloaded the prefix increment. This means we can do stuff like this:
Notice how the return type is the object. That's because we use the increment like this:
So in the first example, we incremented the object. The object returned itself, but it wasn't being used by anything, so the return type isn't needed there.
But, the second example saw us printing the object, which means we needed a return type there. That's why the increment operator needs a return type.
Let's look at another example.
As you can see, this operator overload returns type
bool, because it's making a comparison. Also, I added the
const there because I'm trying to get into the habit of using
const cause... idk, better programming practice?
Now hopefully you guys get the jist of things. Now were going to take a look at what we're going to take a look at :)
Unary Operator - operates on one value
++ (pre- and postfix)
-- (pre- and postfix)
Binary Operators - operates on two values
 (subscript operator)
() (function operator)
Increments and Decrements
Notice how the return type is the object. This is because we use increments like this:
cout << ++value;. If it didn't return anything, then
cout would be printing a
Notice how the coversion operator just turns the object into an int. It's kinda special since it doesn't have a return type.
Oh, and just a side note, the conversion operator is very powerful. In some cases, that's the only overloaded operator you'll need! Because remeber, it takes an object, and turns it into an 'int'. Very useful.
Returns an object for the same reason that the increments return an object.
Notice how the return type is void. This is because we don't need to return a value when working with assignment operators.
The return type is expected.
The return type is expected.
Again, doesn't return a type because it doesn't need too.
Okay, now you know what overloading operators are, let's take a look at a real example.
Oh, and what's up with all the squids? Well I'm making my own data type actually called "squid" (coming out soon), so yeah...
And that's it! Most of the operators you'll ever need. If there's one that I missed, please let me know so I can add it in.
And don't forget to upvote :)
@DynamicSquid ive been stressing for the past 20 minutes because I have a 5MB file (the linux binary for a fixed version of dusk i need to roll out) that won't appear in the downloaded zip, and my linux dualboot has been borked, so what do I do? (yes, I know, I'll reported this to @amasad but really?)
If there's one that I missed, please let me know so I can add it in.
All logical and bit-wise operators.
I'm trying to get into the habit of using
Nice! I make sure to use immutable data wherever possible.
using namespace std;
And... I just lost all respect for you!
Also, this reminds me of D, which made operator overloading 100x easier, wanna see an example? Oh yeah, I forgot how to code in D... lol :(
D > C++ > C
I honestly think if the tutorial is too long and not interactive enough, people won't care. I'ma make an interactive C++ tutorial, how about that!?