operator overloading
Customizes the C++ operators for operands of userdefined types.
Contents 
Syntax
Overloaded operators are functions with special function names:
operator op

(1)  
operator type

(2)  
operator new

(3)  
operator delete

(4)  
operator "" suffixidentifier

(5)  (since C++11)  
Overloaded operators
When an operator appears in an expression, and at least one of its operands has a class type or an enumeration type, then overload resolution is used to determine the userdefined function to be called among all the functions whose signatures match the following:
Expression  As member function  As nonmember function  Example 

@a  (a).operator@ ( )  operator@ (a)  !std::cin calls std::cin.operator!() 
a@b  (a).operator@ (b)  operator@ (a, b)  std::cout << 42 calls std::cout.operator<<(int) 
a=b  (a).operator= (b)  cannot be nonmember  std::string s; s = "abc"; calls std::string.operator=(const char*) 
a[b]  (a).operator[](b)  cannot be nonmember  std::map<int, int> m; m[1] = 2; calls m.operator[](int) 
a>  (a).operator> ( )  cannot be nonmember  std::unique_ptr<S> ptr(new S); ptr>bar() calls ptr.operator>() 
a@  (a).operator@ (0)  operator@ (a, 0)  std::vector<int>::iterator i = v.begin(); i++ calls i.operator++(0) 
Note: for overloading userdefined conversion functions, userdefined literals, allocation and deallocation see their respective articles.
Overloaded operators (but not the builtin operators) can be called using function notation:
std::string str = "Hello, "; str.operator+=("world"); // same as str += "world"; operator<<(operator<<(std::cout, str) , '\n'); // same as std::cout << str << '\n';
Restrictions
 The operators
::
(scope resolution),.
(member access),.*
(member access through pointer to member), and?:
(ternary conditional) cannot be overloaded  New operators such as
**
,<>
, or&
cannot be created  The overloads of operators
&&
,
, and,
(comma) lose their special properties: shortcircuit evaluation and sequencing.
Canonical implementations
The language puts no restrictions on what the overloaded operators do, or on the return type (it does not participate in overload resolution), but in general, overloaded operators are expected to behave as similar as possible to the builtin operators: operator+ is expected to add, rather than multiply its arguments, operator= is expected to assign, etc. The related operators are expected to behave similarly (operator+ and operator+= do the same additionlike operation). The return types are limited by the expressions in which the operator is expected to be used: for example, assignment operators return by reference to make it possible to write a=b=c=d, because the builtin operators allow that. Commonly overloaded operators have the following typical, canonical forms:^{[1]}
Assignment operator
The assignment operator (operator=) has special properties: see copy assignment and move_assignment for details. To summarize, the canonical "universal assignment operator" implementation is
T& T::operator=(T arg) { // copy/move constructor is called to construct arg swap(arg); // resources exchanged between *this and arg return *this; } // destructor is called to release the resources formerly held by *this
When there are resources that can be reused in assignment, for example, if the class owns a heapallocated array, then copyassignment between arrays of the same size can avoid allocation and deallocation:
T& operator=(const T& other) // copy assignment { if (this != &other) { // selfassignment check expected if (/* storage cannot be reused (e.g. different sizes) */) { delete[] mArray; // destroy storage in this mArray = new int[/*size*/]; // create storage in this } // copy data from other's storage to this storage } return *this; } T& operator=(T&& other) // move assignment { assert(this != &other); // selfassignment check not required delete[] mArray; // delete this storage mArray = other.mArray; // move other.mArray = nullptr; // leave movedfrom in valid state return *this; }
Stream extraction and insertion
The overloads of operator>>
and operator<<
that take a std::istream& or std::ostream& as the left hand argument are known as insertion and extraction operators. Since they take the userdefined type as the right argument (b
in a@b), they must be implemented as nonmembers.
std::ostream& operator<<(std::ostream& os, const T& obj) { // write obj to stream return os; } std::istream& operator>>(std::istream& is, T& obj) { // read obj from stream if( /* T could not be constructed */ ) is.setstate(std::ios::failbit); return is; }
These operators are sometimes implemented as friend functions.
Function call operator
When a userdefined class overloads the function call operator, operator(), it becomes a FunctionObject
type. Many standard algorithms, from std::sort to std::accumulate accept objects of such types to customize behavior. There are no particularly notable canonical forms of operator(), but to illustrate the usage
struct Sum { int sum; Sum() : sum(0) {} void operator()(int n) { sum += n; } }; Sum s = std::for_each(v.begin(), v.end(), Sum());
Increment and decrement
When the postfix increment and decrement appear in an expression, the corresponding userdefined function (operator++ or operator) is called with an integer argument 0
. Typically, it is implemented as T operator++(int), where the argument is ignored. The postfix increment and decrement operator is usually implemented in terms of the prefix version:
struct X { X& operator++() { // actual increment takes place here return *this; } X operator++(int) { X tmp(*this); // copy operator++(); // preincrement return tmp; // return old value } };
Although canonical form of preincrement/predecrement returns a reference, as with any operator overload, the return type is userdefined; for example the overloads of these operators for std::atomic return by value.
Binary arithmetic operators
Binary operators are typically implemented as nonmembers to maintain symmetry (for example, when adding a complex number and an integer, if operator+
is a member function of the complex type, then only complex+integer would compile, and not integer+complex). Since for every binary arithmetic operator there exists a corresponding compound assignment operator, canonical forms of binary operators are implemented in terms of their compound assignments:
class X { X& operator+=(const X& rhs) // compound assignment (does not need to be a member, { // but often is, to modify the private members) // addition of rhs to *this takes place here return *this; // return the result by reference } }; inline X operator+(X lhs, const X& rhs) // first arg by value, second by const ref { return lhs += rhs; // reuse compound assignment and return the result by value }
Relational operators
Standard algorithms such as std::sort and containers such as std::set expect operator< to be defined, by default, for the userprovided types. Typically, operator< is provided and the other relational operators are implemented in terms of operator<.
inline bool operator< (const X& lhs, const X& rhs){ /* do actual comparison */ } inline bool operator> (const X& lhs, const X& rhs){return rhs < lhs;} inline bool operator<=(const X& lhs, const X& rhs){return !(lhs > rhs);} inline bool operator>=(const X& lhs, const X& rhs){return !(lhs < rhs);}
Likewise, the inequality operator is typically implemented in terms of operator==:
inline bool operator==(const X& lhs, const X& rhs){ /* do actual comparison */ } inline bool operator!=(const X& lhs, const X& rhs){return !(lhs == rhs);}
Array subscript operator
Userdefined classes that provide arraylike access typically define two overloads for operator[]: one which can be called in rvalue context such as x = a[i];, and one which can be used in lvalue context, such as a[i] = x;.
struct T { value_t& operator[](std::size_t idx) { /* actual access, e.g. return mVector[idx]; */ }; const value_t& operator[](std::size_t idx) const { // either actual access, or reuse nonconst overload // for example, as follows: return const_cast<T&>(*this)[idx]; }; };
To provide multidimensional array access semantics, e.g. to implement a 3D array access a[i][j][k] = x;, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] which returns a reference to the element. To avoid this complexity, some libraries opt for overloading operator() instead, so that 3D access expressions have the Fortranlike syntax a(i,j,k) = x;
Example
#include <iostream> class Fraction { int gcd(int a, int b) {return b==0 ? a : gcd(b,a%b); } int n, d; public: Fraction(int n, int d = 1) : n(n/gcd(n,d)), d(d/gcd(n,d)) {} int num() const { return n; } int den() const { return d; } Fraction& operator*=(const Fraction& rhs) { int new_n = n*rhs.n / gcd(n*rhs.n, d*rhs.d); d = d*rhs.d / gcd(n*rhs.n, d*rhs.d); n = new_n; return *this; } }; std::ostream& operator<<(std::ostream& out, const Fraction& f){ return out << f.num() << '/' << f.den() ; } bool operator==(const Fraction& lhs, const Fraction& rhs) { return lhs.num() == rhs.num() && lhs.den() == rhs.den(); } bool operator!=(const Fraction& lhs, const Fraction& rhs) { return !(lhs == rhs); } Fraction operator*(Fraction lhs, const Fraction& rhs) { return lhs *= rhs; } int main() { Fraction f1(3,8), f2(1,2), f3(10,2); std::cout << f1 << '*' << f2 << '=' << f1*f2 << '\n' << f2 << '*' << f3 << '=' << f2*f3 << '\n' << 2 << '*' << f1 << '=' << 2 *f1 << '\n'; }
Output:
3/8*1/2=3/16 1/2*5/1=5/2 2*3/8=3/4
See Also
Common operators  

assignment  increment decrement 
arithmetic  logical  comparison  member access 
other 
a = b 
++a 
+a 
!a 
a == b 
a[b] 
a(...) 
Special operators  
static_cast converts one type to another related type 
References
 ↑ Operator Overloading on StackOverflow C++ FAQ