Operator Overloading


The latest version of this topic can be found at Operator Overloading.

The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands.

type operator operator-symbol ( parameter-list )  

You can redefine the function of most built-in operators globally or on a class-by-class basis. Overloaded operators are implemented as functions.

The name of an overloaded operator is operator``x, where x is the operator as it appears in the following table. For example, to overload the addition operator, you define a function called operator+. Similarly, to overload the addition/assignment operator, +=, define a function called operator+=.

Redefinable Operators

!Logical NOTUnary
%=Modulus assignmentBinary
&Bitwise ANDBinary
&&Logical ANDBinary
&=Bitwise AND assignmentBinary
( )Function call
( )Cast OperatorUnary
*Pointer dereferenceUnary
*=Multiplication assignmentBinary
+Unary PlusUnary
++Increment 1Unary
+=Addition assignmentBinary
Unary negationUnary
––Decrement 1Unary
–=Subtraction assignmentBinary
–>Member selectionBinary
–>*Pointer-to-member selectionBinary
/=Division assignmentBinary
<Less thanBinary
<<Left shiftBinary
<<=Left shift assignmentBinary
<=Less than or equal toBinary
>Greater thanBinary
>=Greater than or equal toBinary
>>Right shiftBinary
>>=Right shift assignmentBinary
[ ]Array subscript
^Exclusive ORBinary
^=Exclusive OR assignmentBinary
&#124;Bitwise inclusive ORBinary
&#124;=Bitwise inclusive OR assignmentBinary
&#124;&#124;Logical ORBinary
~One's complementUnary
conversion operatorsconversion operatorsUnary

1 Two versions of the unary increment and decrement operators exist: preincrement and postincrement.

See General Rules for Operator Overloading for more information. The constraints on the various categories of overloaded operators are described in the following topics:

The operators shown in the following table cannot be overloaded. The table includes the preprocessor symbols # and ##.

Nonredefinable Operators

.Member selection
.*Pointer-to-member selection
::Scope resolution
? :Conditional
#Preprocessor convert to string
##Preprocessor concatenate

Although overloaded operators are usually called implicitly by the compiler when they are encountered in code, they can be invoked explicitly the same way as any member or nonmember function is called:

Point pt;  
pt.operator+( 3 );  // Call addition operator to add 3 to pt.  

The following example overloads the + operator to add two complex numbers and returns the result.

// operator_overloading.cpp  
// compile with: /EHsc  
#include <iostream>  
using namespace std;  
struct Complex {  
   Complex( double r, double i ) : re(r), im(i) {}  
   Complex operator+( Complex &other );  
   void Display( ) {   cout << re << ", " << im << endl; }  
   double re, im;  
// Operator overloaded using a member function  
Complex Complex::operator+( Complex &other ) {  
   return Complex( re + other.re, im + other.im );  
int main() {  
   Complex a = Complex( 1.2, 3.4 );  
   Complex b = Complex( 5.6, 7.8 );  
   Complex c = Complex( 0.0, 0.0 );  
   c = a + b;  

6.8, 11.2  

  1. General Rules for Operator Overloading

  2. Overloading Unary Operators

  3. Binary Operators

  4. Assignment

  5. Function Call

  6. Subscripting

  7. Member Access

C++ Operators