Operators use in c++
For the purposes of this table, a
, b
, and c
represent valid values (literals, values from variables, or return
value), object names, or lvalues, as appropriate.
"Can overload" means that the operator can be overloaded in C++.
"Included in C" means that the operator exists and has a semantic
meaning in C (operators are not overloadable in C).
Arithmetic operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T is any type) |
As member of T |
Outside class definitions |
Basic assignment |
a = b |
Yes |
Yes |
T&
T::operator =(const
T& b); |
N/A |
Addition |
a + b |
Yes |
Yes |
T T::operator +(const T& b) const; |
T operator +(const T&
a, const T& b); |
Subtraction |
a - b |
Yes |
Yes |
T T::operator -(const T& b) const; |
T operator -(const T&
a, const T& b); |
Unary plus (integer promotion) |
+a |
Yes |
Yes |
T T::operator +() const; |
T operator +(const T&
a); |
Unary minus (additive inverse) |
-a |
Yes |
Yes |
T T::operator -() const; |
T operator -(const T&
a); |
Multiplication |
a * b |
Yes |
Yes |
T T::operator *(const T& b) const; |
T operator *(const T&
a, const T& b); |
Division |
a / b |
Yes |
Yes |
T T::operator /(const T& b) const; |
T operator /(const T&
a, const T& b); |
Modulo (integer remainder)[note
1] |
a % b |
Yes |
Yes |
T T::operator %(const T& b) const; |
T operator %(const T&
a, const T& b); |
Increment |
Prefix |
++a |
Yes |
Yes |
T&
T::operator ++(); |
T&
operator ++(T& a); |
Suffix |
a++ |
Yes |
Yes |
T T::operator ++(int); |
T operator ++(T& a, int); |
Note: C++ uses the
unnamed dummy-parameter int to differentiate between prefix and suffix
increment operators. |
Decrement |
Prefix |
--a |
Yes |
Yes |
T&
T::operator --(); |
T&
operator --(T& a); |
Suffix |
a-- |
Yes |
Yes |
T T::operator --(int); |
T operator --(T& a, int); |
Note: C++ uses the
unnamed dummy-parameter int to differentiate between prefix and suffix
decrement operators. |
Comparison
operators/relational operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T is any type) |
As member of T |
Outside class definitions |
Equal to |
a == b |
Yes |
Yes |
bool
T::operator ==(const
T& b) const; |
bool
operator ==(const T& a, const T& b); |
Not equal to |
a != b |
Yes |
Yes |
bool T::operator !=(const T& b) const; |
bool
operator !=(const T&
a, const T& b); |
Greater than |
a > b |
Yes |
Yes |
bool T::operator >(const
T& b) const; |
bool
operator >(const T& a, const T& b); |
Less than |
a < b |
Yes |
Yes |
bool T::operator <(const
T& b) const; |
bool
operator <(const T& a, const T& b); |
Greater than or equal to |
a >= b |
Yes |
Yes |
bool T::operator >=(const
T& b) const; |
bool
operator >=(const T& a, const T& b); |
Less than or equal to |
a <= b |
Yes |
Yes |
bool T::operator <=(const
T& b) const; |
bool
operator <=(const T& a, const T& b); |
Logical operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T is any type) |
As member of T |
Outside class definitions |
Logical
negation (NOT) |
!a |
Yes |
Yes |
bool
T::operator !()
const; |
bool
operator !(const T& a); |
Logical AND |
a && b |
Yes |
Yes |
bool T::operator &&(const
T& b) const; |
bool
operator &&(const T& a, const T& b); |
Logical OR |
a || b |
Yes |
Yes |
?bool
T::operator ||(const
T& b) const;? |
?bool
operator ||(const T& a, const T& b);? |
Bitwise operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T is any type) |
As member of T |
Outside class definitions |
Bitwise NOT |
~a |
Yes |
Yes |
T T::operator ~() const; |
T operator ~(const T&
a); |
Bitwise AND |
a & b |
Yes |
Yes |
T T::operator &(const T&
b) const; |
T operator &(const T&
a, const T& b); |
Bitwise OR |
a | b |
Yes |
Yes |
T T::operator |(const T& b) const; |
T operator |(const T&
a, const T& b); |
Bitwise XOR |
a ^ b |
Yes |
Yes |
T T::operator ^(const T& b) const; |
T operator ^(const T&
a, const T& b); |
Bitwise left shift[note 2] |
a << b |
Yes |
Yes |
T T::operator <<(const T&
b) const; |
T operator <<(const T&
a, const T& b); |
Bitwise right shift[note 2][note 3] |
a >> b |
Yes |
Yes |
T T::operator >>(const T&
b) const; |
T operator >>(const T&
a, const T& b); |
Compound
assignment operators
Operator name |
Syntax |
Meaning |
Can overload |
Included
in C |
Prototype examples (T is any type) |
As member of T |
Outside class definitions |
Addition assignment |
a += b |
a = a + b |
Yes |
Yes |
T&
T::operator +=(const T& b); |
T&
operator +=(T& a, const
T& b); |
Subtraction assignment |
a -= b |
a = a - b |
Yes |
Yes |
T&
T::operator -=(const T& b); |
T&
operator -=(T& a, const
T& b); |
Multiplication assignment |
a *= b |
a = a * b |
Yes |
Yes |
T&
T::operator *=(const T& b); |
T&
operator *=(T& a, const
T& b); |
Division assignment |
a /= b |
a = a / b |
Yes |
Yes |
T&
T::operator /=(const T& b); |
T&
operator /=(T& a, const
T& b); |
Modulo assignment |
a %= b |
a = a % b |
Yes |
Yes |
T&
T::operator %=(const T& b); |
T&
operator %=(T& a, const
T& b); |
Bitwise AND assignment |
a &= b |
a = a & b |
Yes |
Yes |
T&
T::operator &=(const T& b); |
T&
operator &=(T& a, const
T& b); |
Bitwise OR assignment |
a |= b |
a = a | b |
Yes |
Yes |
T&
T::operator |=(const T& b); |
T&
operator |=(T& a, const
T& b); |
Bitwise XOR assignment |
a ^= b |
a = a ^ b |
Yes |
Yes |
T&
T::operator ^=(const T& b); |
T&
operator ^=(T& a, const
T& b); |
Bitwise left shift assignment |
a <<= b |
a = a << b |
Yes |
Yes |
T&
T::operator <<=(const
T& b); |
T&
operator <<=(T& a, const T& b); |
Bitwise right shift assignment[note 3] |
a >>= b |
a = a >> b |
Yes |
Yes |
T&
T::operator >>=(const
T& b); |
T&
operator >>=(T& a, const T& b); |
Member and
pointer operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T, T2 and R are any type) |
As member of T |
Outside class definitions |
Array subscript |
a[b] |
Yes |
Yes |
R&
T::operator [](const T2& b);
|
N/A |
Indirection ("object pointed to by a") |
*a |
Yes |
Yes |
R&
T::operator *(); |
R&
operator *(T& a); |
Reference ("address of a") |
&a |
Yes |
Yes |
T* T::operator &(); |
T*
operator &(T& a); |
Structure dereference ("member b of object
pointed to by a") |
a->b |
Yes |
Yes |
R* T::operator ->();
|
N/A |
Structure reference ("member b of object a") |
a.b |
No |
Yes |
N/A |
Member pointed to by b of object pointed to by a[note 4] |
a->*b |
Yes |
No |
R T::operator->*(R);[note 5] |
R operator->*(T,
R);[note 5] |
Member pointed to by b of object a |
a.*b |
No |
No |
N/A |
Other operators
Operator name |
Syntax |
Can overload |
Included
in C |
Prototype examples (T, R, Arg1 and Arg2 are any type) |
As member of T |
Outside class definitions |
Function call
See Function object. |
a(a1, a2) |
Yes |
Yes |
R T::operator ()(Arg1 a1, Arg2 a2, ...); |
N/A |
Comma |
a, b |
Yes |
Yes |
R&
T::operator ,(R& b)
const; |
R&
operator ,(const T& a, R& b); |
Ternary
conditional |
a ? b : c |
No |
Yes |
N/A |
Scope resolution |
a::b |
No |
No |
N/A |
Size-of |
sizeof(a) [note
6]
sizeof(type) |
No |
Yes |
N/A |
Align-of |
alignof(type)
or _Alignof(type) [note 7] |
No |
Yes |
N/A |
Type identification |
typeid(a)
typeid(type) |
No |
No |
N/A |
Cast |
(type) a |
Yes |
Yes |
T::operator R() const; |
N/A |
Note: for user-defined
conversions, the return type implicitly and necessarily matches the
operator name. |
Allocate storage |
new type |
Yes |
No |
void* T::operator
new(size_t
x); |
void* operator new(size_t x); |
Allocate storage (array) |
new type[n] |
Yes |
No |
void* T::operator
new[](size_t x); |
void* operator new[](size_t x); |
Deallocate storage |
delete a |
Yes |
No |
void T::operator delete(void* x); |
void
operator delete(void* x); |
Deallocate storage (array) |
delete[] a |
Yes |
No |
void T::operator delete[](void*
x); |
void
operator delete[](void* x); |
Notes:
- ^ The modulus operator works just with integer
operands, for floating point numbers a library function must be used
instead (like fmod).
- ^ a b In the context of iostreams, writers often will refer to <<
and >>
as the “put-to” or "stream insertion" and “get-from” or "stream
extraction" operators, respectively.
- ^ a b
According to the C99 standard, the right
shift of a negative number is implementation defined. Most
implementations, e.g., the GCC,
use an arithmetic shift (i.e., sign extension),
but a logical shift is possible.
- ^ An example can be found in "Implementing
operator->* for Smart Pointers" by Scott Meyers.
- ^ a b In the case where the ->* operator is to work
just like the default implementation, the R parameter will be method
pointer to a method of the class T and the return value must be some
kind of functor object that is ready to be called with (only) the method
parameters.
- ^ The parentheses are not necessary when taking the
size of a value, only when taking the size of a type. However, they are
usually used regardless.
- ^ C++ defines
alignof
operator,
whereas C defines _Alignof
. Both operators have the same
semantics.
Reference: wikipedia.org
J.W. Production