Namespaces
Variants
Views
Actions

operators

From cppreference.com
< cpp‎ | language
Revision as of 21:36, 2 November 2012 by P12bot (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Contents

Operator Overloading

Syntax

Template:sparam operator Template:sparam ( Template:sparam ) ;

Explanation

  • <type> is/are the type(s) of the variables.
  • <op> is the particular operator (e.g. +, +=, <<, >>, &&, ||, %, etc.).
  • <params> is/are the name(s) of the required parameters (depends on the operator).

Restrictions

  • You cannot create new operators such as ** or &|.
  • Not all operators can be overloaded
  • Some operators can only be overloaded as non-static class members
  • Short-circuit evaluation doesn't work with overloaded operators

Operator Calls

Overloaded operators can be called using the usual infix notation

a+b

or a function-like notation

operator+(a,b)

Example

#include <iostream>
 using namespace std;
 
 class Fraction{
   private:
     int numerator, denominator;
 
   public:
     Fraction(int n, int d): numerator(n), denominator(d) {}
   // Note that the keyword operator combined with an actual
   // operator is used as the function name
   friend ostream& operator<<(ostream&, Fraction&);
 };
 
 ostream& operator<<(ostream& out, Fraction& f){
   out << f.numerator << '/' << f.denominator;
   return out;
 }
 
 int main(){
   Fraction f1(3, 8);
   Fraction f2(1, 2);
 
   cout << f1 << endl;
   cout << 3 << ' ' << f2 << endl;
 
   return 0;
 }

Output:

3/8
3 1/2

See Also

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)