Operator Overloading
Introduction to Operator Overloading
What is operator overloading? Operator overloading is the practice of assigning new meaning to existing C++ operators when they are used with user defined types…
Arithmetic and Comparison Operators
Overloaded arithmetic and comparison operators are used the most frequently. When it's obvious that an object represents a value that's added, subtracted, or co…
Assignment Operators
Assignment operators differ from the arithmetic operators previously shown. The expression a + b returns a new value and leaves a and b unchanged; the expressio…
Stream Input and Output Operators
The stream input and output operators allow the standard I/O operators of std::cin, std::cout, and the other standard streams to operate on user defined types.…
Subscript and Function Call Operators
Subscript and Function Call Operators The subscript operator [] and the function call operator () allow a class object to behave like an array or like a functio…
Conversion Operators
A conversion operator allows your class objects to be implicitly converted to other types. They are defined like normal member functions, but are named: operato…
The Three-Way Comparison Operator
The three way comparison operator, written as <=> and commonly known as the spaceship operator, was introduced in C++20. One call of the operator will determine…
When Operator Overloading Should Be Avoided
Operator overloading can be incredibly useful, and equally easily misused. As soon as an overloaded operator doesn’t represent the intuitive meaning of its symb…