C++ Core
Operators & Operator Overloading
Define what operators mean for your own type — chosen by operand types at compile time.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- An operator is a function with a special name (
operator+,operator==,operator<<); overloading = defining what it does for your type. It's compile-time polymorphism (the compiler picks the function from the operand types). - Two forms: member (left operand is
this) and free function (both operands are params). Use a free function when the left operand isn't your class (cout << e,2 * v). operator<<must be a free function (left operand is the stream, which you don't own) and returnsstd::ostream&so calls chain (cout << a << b).- Params as
const T&(reference = no copy, const = don't modify the argument); trailingconston a member = doesn't modifythis. - Return by value for arithmetic (
+makes a NEW object); return*thisby reference for assignment/compound (=,+=) soa = b = cchains. - Limits: can't invent operators or change precedence/arity; at least one operand must be user-defined (
int+intcan't be redefined); can't overload::,.,.*,?:,sizeof.
Examples: + (by value), = (by reference), << (free)
class Entity {
int a;
public:
Entity(int a = 0) : a(a) {}
// arithmetic: makes a NEW object -> return BY VALUE
Entity operator+(const Entity& rhs) const {
return Entity(a + rhs.a);
}
// assignment: modifies THIS object -> return *this BY REFERENCE (chains a = b = c)
Entity& operator=(const Entity& rhs) {
a = rhs.a;
return *this;
}
friend std::ostream& operator<<(std::ostream& os, const Entity& e);
};
// stream: FREE function, returns the stream so << can chain
std::ostream& operator<<(std::ostream& os, const Entity& e) {
os << e.a;
return os;
}
Entity x(4), y(5), z;
z = x + y; // operator+ (new object) then operator= (into z)
std::cout << z << std::endl; // operator<< -> prints 9 Watch out
Easily confused
member vs free operator
Member: left operand is
this. Free: both operands are params — required when the left operand isn't your type (cout << e, 2 * v).+ returns by value vs = returns by reference
+ makes a NEW object -> return by value. = modifies the existing object -> return *this; by reference (enables a = b = c).overloadable vs not
Can't redefine
int+int (both built-in), can't invent new operators or change precedence; can't overload :: . .* ?: sizeof.Test yourself
7 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.