HOWTO · C++
How to Overload the == Operator in C++
This is a programming tutorial for the implementation of the == operator overloading in C++.
On this page
A class can define equality by implementing operator==. The function should compare the fields that represent the object’s value. The Employee example below compares a name and salary, then shows how == reports matching and differing employees.
Operator Overloading in C++
Operator overloading gives an existing operator a meaning for a user-defined type, such as a class or struct. A member overload commonly has this form: return_type operator symbol(parameters). For example, operator== is the function name used to overload equality.
An overload must involve at least one user-defined type; it cannot redefine an operation whose operands are all built-in types such as int or char. It also cannot change an operator’s precedence, number of operands, or associativity. The syntax and familiar expression remain the same, so callers can write first == second instead of calling a comparison method directly.
Some operators cannot be overloaded, including ::, ., .*, ?:, and sizeof. The typeid operator also cannot be overloaded.
Overload the == Operator in C++
For a class, decide which fields define equality before writing the function. This Employee value uses both name and salary; if either differs, the objects compare unequal. A const member function and a const reference parameter allow comparison without copying or modifying either object.
#include <iostream>
#include <string>
class Employee {
public:
Employee(std::string name, int salary)
: name_(name), salary_(salary) {}
bool operator==(const Employee& other) const {
return name_ == other.name_ && salary_ == other.salary_;
}
private:
std::string name_;
int salary_;
};
int main() {
const Employee first("David", 1000);
const Employee same("David", 1000);
const Employee different_salary("David", 1200);
std::cout << (first == same ? "Equal" : "Not equal") << '\n';
std::cout << (first == different_salary ? "Equal" : "Not equal") << '\n';
}
Equal
Not equal
The output shows Equal when both selected fields match and Not equal when the salary differs. If the class later gains another value field, include it when that field should affect equality.
Overload the == Operator in Class Hierarchy
A base-class equality function checks only the base-class state included in its implementation. It may be callable for derived objects through the inherited member, but it does not automatically inspect members added by a derived class. A derived type does not have to define equality unless its own comparison semantics need to include additional state.
For example, if A stores foo and defines bool operator==(const A& other) const, then that function compares foo. If B : public A adds bar, define a B comparison when equality for B should include bar: its result can combine A::operator==(static_cast<const A&>(other)) with bar == other.bar. This explicitly compares the base portion and the derived field. When a derived overload has the same name, it can hide base overloads during ordinary lookup; use a deliberate overload set if both comparisons should be available.
The expression’s static operand types determine which overloads participate. For polymorphic base references, a base comparison still compares the base view; it does not dynamically discover and compare derived members. Equality across a polymorphic hierarchy therefore needs an explicit policy for whether different dynamic types can compare equal, with matching behavior in both operand orders. Keep the rule consistent so equality remains symmetric and reflects the values your type promises to represent.