Using Overloaded Operators


Overloading operators can be very useful in making your programs easier to read, write, maintian, and understand. However, they can also just as easily obscure the meaning of your code if not used carefully. Here are some things to watch out for:

  • Conflicting meanings. It is entirely possible to overload the ++ operator for a class and make it do anything you want it to. However, someone reading your code later is goign to assume that the ++ operator does something to the class which is analogous to incrementing it by one.
  • Multiple overloaders for one class. It is also possible to create more than one operator overloader method in a class as long as each one has a different parameter type. For example, you could create a String class, and then have multiple versions of the + operator, one which takes a single character as a parameter, one which takes another String class, etc. Again, you just need to be careful that your use of overloaded operators makes your code clearer, not more complex.
  • Not all operators can be overloaded. Most operators can be overloaded, but there are a few exceptions, which are:
    • The ternary operator ?:
    • The class/struct member access operator .
    • The scope resolution operator ::


Table of Contents

Operator Overloading | Binary Operator Overloading | Unary Operator Overloading | Overloading Conversion Operators
Using Overloaded Operators | Exercises | Continuing the Journey