Module 6 · Inheritance and Polymorphism — topic 5 of 5
Operator Overloading
Give your types natural syntax: +, ==, <, <<, [] and more. Learn member vs non-member operators, return conventions and C++20's spaceship operator.
Learning objectives
- Overload arithmetic, comparison and stream operators
- Choose member or non-member implementations
- Follow the conventional return types
Definition
Operator overloading defines the meaning of built-in operators for user-defined types by writing functions named operator@, which the compiler calls when the operator is applied to objects of those types.Introduction
std::string lets you write a + b, a == b and std::cout << a because those operators are overloaded. Your own types — vectors, fractions, money — can be just as natural to use.
Why it matters
Well-chosen operators make mathematical and value types readable: p + q instead of p.add(q).
Overloading < or == lets your types work with std::sort, std::find, std::set and std::map.
Detailed explanation
Write T operator+(const T& a, const T& b) as a non-member (often a friend) so conversions apply to both operands. Implement it in terms of operator+=, which is a member returning T&.
Comparison: bool operator==(const T&, const T&), bool operator<(...). In C++20, auto operator<=>(const T&) const = default; generates all six comparisons.
Streams: std::ostream& operator<<(std::ostream& os, const T& t) must be a non-member and should return os for chaining.
operator[] must be a member; provide const and non-const versions. Assignment operator= returns T&.
Don't overload &&, || or , (they lose short-circuit/sequencing), and keep meanings intuitive.
Syntax
- Compound assignment is a member returning
*this. - Binary arithmetic is usually a non-member.
<<returns the stream.
Code examples
A 2D vector type
(4, 6) (6, 8)
Comparison for sorting
Raj Zoe Ana
More examples
A Fraction with arithmetic, comparison and output
5/6 1/6 3/2 true true 4/5
Step-by-step explanation
- Compound operators update and normalise the fraction.
- Binary
+and*are friends built from the compound ones;a + 1works because 1 converts to Fraction(1). ==compares normalised values;<cross-multiplies (denominators are positive).- The telescoping sum 1/2 + 1/6 + 1/12 + 1/20 equals 4/5.
Expected output
Running “A Fraction with arithmetic, comparison and output” prints exactly:
Practical applications
- Math libraries (Eigen, GLM) overload operators for vectors and matrices.
std::chronodurations support+,-and comparisons.- Big-integer and money classes overload arithmetic.
Best practices
- Implement
+in terms of+=. - Keep operator meanings intuitive.
- In C++20, default the spaceship operator for member-wise comparison.
Common mistakes
operator<< as a member
Avoid
Prefer
As a member it would be called as p << std::cout. The stream must be the left operand.
Returning by value from operator+=
Avoid
Prefer
Compound assignment should return a reference so (a += b) += c modifies a.
Important notes
- C++20:
auto operator<=>(const T&) const = default;plusbool operator==(const T&) const = default;generate all comparisons. - You cannot create new operators or change precedence and arity.
Key takeaways
- Operators are functions named
operator@. - Members:
+=,=,[]; non-members:+,==,<<. <<returns the stream for chaining.
Try it yourself
- 1Add
-,/and!=to the Fraction class. - 2Write a
Matrix2x2class with*and<<operators.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhy is `operator<<` for printing usually a non-member function?
- ModerateWhat is printed?