Operator Overloading C++

In a C++ class, it is possible to change the functionality of operators to suit the needs of the class.  This is called “Operator Overloading”.  Operators that can be overloaded include +, -, *, /, +=, -=, *=, /=, ==, <, >, <=, >=, !=, and a number of others.  There are actually only a few operators that can’t be overloaded.

A situation where overloading might be used would be a vector class.  A vector class might have member variables for x, y, and z.  When two vectors are added, the x, y, and z values should be summed.  The + operator could be overloaded, so that the x, y, and z member variables get added together when two vectors are added.

 

Leave a Reply

Your email address will not be published. Required fields are marked *