While not directly related to structs, objects, and member functions, operator overloading is an interesting addition that can help us when we build out our dynamic array.
Operator overloading refers to the ability of a programming language to allow programmers to define how operators work with their data types. This allows you to say how thinks like + and - may work, or how array indexing works using [] operators.
Operator Overloading - Why, When, and How
C++ allows you to override a whole range of operators. So we could, for example, override the + operator in our dynamic array to allow you to add elements to it, or we could choose the << operator to achieve the same effect. Both of these are awful ideas.
Operator overloading can be great if you have numeric values you are working with, and the standard mathematical symbols make sense in that context. In most other cases, you should use methods with names that make it clear what you want to achieve.
Some operators you may want to consider overloading are the comparison operators and the array subscript operator. For the moment, we will only look at the array subscript operator and the comparison operators, but similar syntax applies for other operators you can overload.
In C++
Example
The following code add comparison operators to the die, allowing you to compare it with other die (only for <) and to integer values. The different comparison operators are implemented in terms using the code in the less than operator, though in this case it is easy enough to have implemented each with its own code.
Notice that we switched the die onto the stack so that it was easy to get a reference to this. If you retain the pointer, you need to dereference it before you can do the comparison, eg. *d >= 5.