Ever stared at a list of C++ symbols and wondered which of the following operator can be overloaded — and which ones are locked down by the language no matter how hard you try? You're not alone. Most people hit this wall the first time they try to make a custom class feel "native," then get slapped by a compiler error they didn't see coming Worth keeping that in mind..
Here's the thing — operator overloading looks like magic until you realize the rules are weirdly specific. Some operators are fair game. Others are off-limits for reasons that actually make sense once you see the bigger picture.
What Is Operator Overloading
Operator overloading is just letting your own types use the same symbols everyone knows — +, ==, <<, that kind of thing — in a way that makes sense for your object. Instead of writing matrix.add(otherMatrix), you write matrix + otherMatrix. The compiler quietly calls the function you defined.
In practice, it's a way to make user-defined types feel less like second-class citizens. Even so, a string class that can't use + to concatenate feels broken. A vector math library that forces you to call .multiply() instead of using * is annoying to read.
The Basic Idea
You're not really changing what + means to the language. You're writing a function with a funny name — like operator+ — and the compiler maps the symbol to it when the types line up. That's it. No dark arts That alone is useful..
What "Overloadable" Actually Means
When people ask which of the following operator can be overloaded, they usually mean: out of the standard set, which ones can I redefine for my own classes? Consider this: the answer is most of them. But not all. And the ones you can't overload exist for safety or because the language spec says so.
Why It Matters / Why People Care
Why does this matter? Because most people skip the rules and learn by trial and error. That's fine until you ship a bug because you assumed :: could be overloaded and it can't — so you hacked around it and made a mess And it works..
Understanding the boundary saves time. It also stops you from writing code that fights the language. I've seen devs waste a day trying to overload the dot operator in C++ because they wanted prettier member access. Spoiler: you can't. And once you know why, the workaround is obvious.
Real talk — if you're building libraries, APIs, or anything someone else will use, operator support is part of the feel. A well-overloaded << for logging makes your type pleasant. A missing == makes it frustrating. Knowing what's possible is step one But it adds up..
How It Works (or How to Do It)
Let's get into the actual split. The short version is: almost all operators in C++ can be overloaded, but a small set cannot. Here's the meaty part.
Operators You Can Overload
These are the usual suspects. If you've used a modern C++ library, you've seen most of them:
- Arithmetic:
+,-,*,/,%,++,-- - Comparison:
==,!=,<,>,<=,>= - Logical:
&&,||,!(though be careful — see mistakes below) - Bitwise:
&,|,^,~,<<,>> - Assignment:
=,+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>= - Member access:
->,->* - Subscript:
[] - Function call:
() - Comma:
, - New/delete:
new,delete,new[],delete[]
That's a long list. Turns out the language gives you a lot of rope. The trick is not hanging yourself with it.
Operators You Cannot Overload
This is the part most guides get wrong — they either list too few or invent reasons. Here's the real set that's locked:
.(member access operator).*(pointer-to-member access)::(scope resolution)?:(ternary conditional)sizeoftypeidalignof(in C++11 and later)#and##(preprocessor operators — not really language operators, but worth knowing they're untouchable)
So when someone hands you a quiz asking which of the following operator can be overloaded, and the options include :: or ?Worth adding: :, those are your "no" answers. Every time Practical, not theoretical..
Why Those Specific Ones Are Blocked
Look, it's not arbitrary. Because of that, the dot operator . On the flip side, is how the compiler finds members at compile time. Letting you overload it would break the whole model of what a class is. The scope resolution :: is similar — it's about where a name lives, not behavior you can swap.
This is the bit that actually matters in practice.
The ternary ?Here's the thing — : is blocked mostly because of its weird evaluation rules (only one branch runs). Overloading it would mean lying about control flow. And sizeof has to work on types that don't even exist at runtime — overloading it makes no sense.
How You Actually Overload One
Say you've got a Money class. You want a + b. You write:
Money operator+(const Money& a, const Money& b) {
return Money(a.cents + b.cents);
}
Or as a member:
Money Money::operator+(const Money& other) const {
return Money(cents + other.cents);
}
That's the whole mechanism. This leads to the compiler sees a + b, checks types, finds your function, calls it. Done.
Common Mistakes / What Most People Get Wrong
Honestly, this is the part most guides get wrong. They tell you "overload everything" or "never overload." Both are bad takes.
Mistake 1: Overloading && and ||
You can overload them. On top of that, your overloaded version is a normal function call, so both sides evaluate first. The built-in versions short-circuit — they don't evaluate the right side if the left is enough. But you shouldn't. Now, that breaks expectations and can cause bugs. Just don't.
Mistake 2: Assuming . Can Be Overloaded
I know it sounds simple — but it's easy to miss if you learned Python first (where __getattr__ does roughly that). If you want smart pointer behavior, overload -> instead. In C++, . is final. That one's allowed and is how std::unique_ptr works.
Honestly, this part trips people up more than it should.
Mistake 3: Forgetting the Rule of Three/Five
If you overload =, you probably need a copy constructor and destructor too. On top of that, overload = but use the default copy and you'll double-free or leak. The language won't stop you. Experience will, painfully.
Mistake 4: Making Operators Do Weird Things
Overloading * to mean "subtract" is legal. It's also terrible. An operator should do what its symbol implies. Nobody wants to debug a * b that actually divides.
Mistake 5: Thinking sizeof Is Overloadable
It isn't. You can't make it call a function. Here's the thing — people ask because they want runtime-sized things — use a . Even so, if you write sizeof myObject you get the static size. size() method instead.
Practical Tips / What Actually Works
Here's what I'd tell a junior dev if they asked me at lunch.
Keep overloads symmetric. If you write a + b as a free function, don't make b + a fail because the left type isn't yours. Free functions often beat member functions for binary operators — they allow conversion on both sides.
Use const & in parameters unless you're moving. Copying big objects through operators is how you get slow code that looks clean.
Only overload when it reads better. In real terms, a Path class with / for joining is nice. Now, a Config class with % for reload is confusing. Resist the urge Nothing fancy..
For streaming, overload << for std::ostream as a free function. That's how your type prints in logs and tests without special handling. It's one of the highest-value overloads you can write.
And please — test your
overloads like any other code. Write unit tests that exercise both a + b and b + a, mixed-type operations, and edge cases like empty or zero-valued objects. Operator syntax hides complexity, which means bugs hide too.
One more thing worth internalizing: operator overloading is a tool for expressiveness, not cleverness. The best overloads are the ones your coworkers never have to think about — they just read naturally and do the obvious thing. If you find yourself explaining what operator% means in a comment, you've probably overloaded the wrong symbol Small thing, real impact..
Conclusion
C++ operator overloading isn't magic and it isn't dangerous by default — it's a controlled way to make your types feel native to the language. Used with restraint, it turns awkward method calls into clear, readable expressions. Used carelessly, it turns your codebase into a puzzle nobody wants to solve. The rules are simple: prefer free functions for symmetry, respect the semantics of each symbol, never break short-circuit evaluation, and remember that overloading one special member usually drags the rest of the Rule of Three/Five along with it. Write overloads that match intuition, test them like normal functions, and they'll pay you back every time you read your own code That's the whole idea..