Fun overloading * and -> in C++
The C++ language allows the developer to overload the unary operator * dereference operator and -> member access operator.
The unary dereference operator return a reference to some type T and must be a member function of the class and can be overloaded on const and non const. So why bother? Well there are a couple of examples from the standard library which we will use to set the scene. But before jumping into examples, think about what the * operator does with pointers. It dereferences the pointer and returns a lvalue expression referencing to the object that the pointers value stores. This is important as one of the goals of creating your own overloaded operators should be do something that is obvious to the user, the principal of least astonishment.
Lets look at std::optional
Looking at the above code, after testing result1 for having a value, result1 is dereferenced to obtain a reference to
the stored value. This is a great example of overloading the * operator. The example below is an example optional
Let me know what you think of this article in the comment section below!