Assignment Operators in C++
Plain = stores a value, but assignment is an expression in C++ — it yields the assigned value, which is why chains like a = b = 0 work (right-associative) and why = inside a condition compiles (usually a bug). The compound forms (+=, -=, *=, /=, %=, plus bitwise &=, |=, ^=, <<=, >>=) fold an operation into the store, evaluating the left side only once.
For class types, = invokes operator= (copy or move assignment), one of the special member functions the Rule of Three/Five governs. Postfix ++/-- are technically assignment-adjacent too, mutating their operand in place.
= is an expression
a = b = 0 assigns right to left, and the whole thing evaluates to 0 — useful for initialization chains, dangerous inside if conditions.
Compound operators
x += 3 means x = x + 3 with x evaluated once. Every arithmetic and bitwise operator has a compound form.
Assignment vs initialization
int x = 5; at declaration initializes; x = 5; later assigns. For classes these call different functions (constructor vs operator=).
Classes customize =
Copy assignment, move assignment, and their interaction with destructors are the heart of the Rule of Three/Five for resource-owning classes.
#include <iostream> int main() { int a = 0, b = 0; a = b = 10; a += 5; b *= 3; std::cout << a << ", " << b << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.