What Are the C++ Operators?
Operators are the symbols and syntactic forms that perform actions on values and types. They enable arithmetic, logic, comparisons, assignment, indirection, member access, allocation, and more.
Problem
- You need a clear, compact reference to C++ operators with examples.
- You want to know which categories exist, how they’re used, and common pitfalls.
Solutions
- Use the categories below to locate the operator you need.
- Prefer clear code over clever operator tricks.
- Check precedence when mixing operators.
Common operator categories in C++:
- Arithmetic:
+ - * / % - Increment/Decrement:
++ -- - Assignment and Compound Assignment:
= += -= *= /= %= &= |= ^= <<= >>= - Comparison (Relational/Equality):
== != < > <= >= - Logical:
&& || ! - Bitwise:
& | ^ ~ << >> - Member Access:
. -> :: .* ->* - Subscript and Call:
[] () - Indirection and Address-Of:
* & - Ternary (Conditional):
?: - Comma:
, - Size/Alignment/Type Info:
sizeof alignof typeid - Allocation/Deallocation:
new new[] delete delete[] - Casts:
static_cast reinterpret_cast const_cast dynamic_cast
Example use:
#include <iostream>
#include <vector>
struct Point { int x; int y; };
int main() {
int a = 5, b = 2;
int sum = a + b; // arithmetic
bool eq = (sum == 7); // comparison
int mask = 0b0101 | 0b0011; // bitwise OR
sum += mask; // compound assignment
Point p{1, 2};
Point* ptr = &p;
int px = ptr->x; // member access via pointer
std::vector<int> v{1,2,3};
v[1] = 42; // subscript
std::cout << (eq ? "ok" : "no"); // ternary
}
Things to Consider
- Precedence and associativity determine evaluation order; add parentheses for clarity.
&&and||short-circuit; be mindful of side effects.- Overloading changes meaning for user types, not for built-in types.
- Some “operators” are spelled as keywords (
new,delete, casts) but function as operators.
C++ * Operator
The c++ * operator has two primary uses: multiplication and pointer indirection. When applied to numbers, it multiplies values. When applied to a pointer, it dereferences and accesses the object the pointer refers to.
int a = 3, b = 4;
int product = a * b; // multiplication
int x = 10;
int* ptr = &x;
int value = *ptr; // indirection (dereference)
* Operator in C++
The * operator in C++ is context-sensitive. In declarations, it defines a pointer type. In expressions, it dereferences a pointer. In arithmetic, it multiplies numbers.
int y = 5;
int* p = &y; // pointer declaration
int z = *p; // dereference, gives 5
int area = 4 * y; // multiplication
C++ ** Operator
The c++ ** operator does not exist in the language. Unlike Python, C++ does not use ** for exponentiation. You must use std::pow from <cmath> or write your own function.
#include <cmath>
#include <iostream>
int main() {
double result = std::pow(2.0, 3.0); // 2^3 = 8
std::cout << result;
}
Operator Overloading in C++
The exact phrase “operator overloading” in C++ refers to defining custom behavior for operators with user-defined types. You can also see c++ operator overloading and C++ overload operator used to describe the same feature.
- You overload with a function named
operatorX. - You cannot create new symbols; you can only overload existing ones.
- Do not change expected semantics (e.g.,
operator==should model equality).
Example:
struct Vec2 {
float x, y;
Vec2 operator+(const Vec2& rhs) const { return {x + rhs.x, y + rhs.y}; }
bool operator==(const Vec2& rhs) const { return x == rhs.x && y == rhs.y; }
};
Gotchas
- Mixing
&(bitwise AND) with&&(logical AND) by accident. - Using
=where==was intended. - Forgetting that
&&/||short-circuit; side effects in the right operand may not run. - Relying on implicit precedence; add parentheses to be explicit.
- Overloading in ways that violate intuitive meaning (hard to read, easy to misuse).
’—>’ Operator in C++
The ’—>’ operator in C++ is actually a common misconception; there is no standalone --> operator. It is usually parsed as -- (decrement) followed by > or confused with the arrow -> member-access operator.
->accesses a member through a pointer.--x > 0is valid but means “pre-decrement then compare.”
Sources
Further Investigation
-
Operator precedence and associativity tables.
-
Guidelines for
operator<=>(three-way comparison) in modern C++. -
When to provide non-member vs member operator overloads.
TL;DR
- C++ provides operators for arithmetic, logic, comparison, assignment, member access, indirection, allocation, and more.
- Use clear categories and examples to avoid precedence mistakes.
// Quick scan: arithmetic, logical, comparison, assignment
int a = 3, b = 4;
bool ok = (a * b) >= 12 && (a + b) == 7; // parentheses clarify intent