|< C++ .NET Operator Overloading 7 | Main | Supplementary Note 2 >|
C++ OperatorsOperator Precedence and AssociativityC++ Operator Precedence and Associativity TableOperator OverloadingOverloadable OperatorsNon Overloadable OperatorsExampleOverloaded Operators---------------------------------------------- General Rules for Operator OverloadingExampleImplicit BoxingExampleExplicitly Request BoxingUse gcnew to Create Value Types and Use Implicit BoxingUnboxingStandard Conversions and Implicit Boxingstatic KeywordStatic Member Functions |
C++ Operators
The following Table contains C++ operators organized by category.
|
Category |
Description |
Symbol |
|
Additive |
Addition Subtraction |
+ – |
|
Assignment |
Addition Assignment Assignment Bitwise AND Assignment Bitwise exclusive OR Assignment Bitwise inclusive OR Assignment Division Assignment Left shift assignment Modulus Assignment Multiplication Assignment Right shift assignment Subtraction Assignment |
+= = &= ^= |= /= <<= %= *= >>= –= |
|
Bitwise |
Bitwise AND Bitwise exclusive OR Bitwise inclusive OR |
& ^ | |
|
Logical |
Logical AND Logical OR |
&& || |
|
Multiplicative |
Division Modulus Multiplication |
/ % * |
|
Postfix |
Cast Function call Member access Postfix decrement Postfix increment Subscript |
() ( ) . and –> –– ++ [ ] |
|
Relational and Equality |
Equality Greater than or equal to Greater than Less than or equal to Less than Not equal |
== >= > <= < != |
|
Shift |
Left shift Right shift |
<< >> |
|
Unary |
Address-of delete Indirection Logical Negation new One's Complement Prefix decrement Prefix increment sizeof Unary Plus Operator Unary Negation Operator |
& none * ! none ~ –– ++ none + - |
|
Miscellaneous |
Comma Conditional Pointer-to-member Reference Scope resolution |
, ? : .* or –>* & :: |
|
Table 1 |
||
Operator Precedence and Associativity
The C++ language includes all C operators and adds several new operators. Operators specify an evaluation to be performed on one of the following:
▪ One operand (unary operator).
▪ Two operands (binary operator).
▪ Three operands (ternary operator).
Operators follow a strict precedence, which defines the evaluation order of expressions containing these operators. Operators associate with either the expression on their left or the expression on their right; this is called "associativity." The following table shows the precedence and associativity of C++ operators (from highest to lowest precedence). Operators in the same segment of the table have equal precedence and are evaluated in the given order in an expression unless explicitly forced by parentheses.
C++ Operator Precedence and Associativity Table
|
Operator (arranged in one line!) |
Name or Meaning |
Associativity |
|
:: |
Scope resolution |
None |
|
., –>, [ ], ( ), ( ), ++, ––, typeid( ), const_cast, dynamic_cast, reinterpret_cast, static_cast |
Member selection (object), Member selection (pointer), Array subscript, Function call member initialization, Postfix increment, Postfix decrement, type name, Type cast (conversion), Type cast (conversion), Type cast (conversion), Type cast (conversion). |
Left to right. |
|
sizeof, ++, ––, ~, !, –, +, &, *, new, delete, () |
Size of object or type, Prefix increment, Prefix decrement, One's complement, Logical not, Unary minus, Unary plus, Address-of, Indirection, Create object, Destroy object, Cast |
Right to left. |
|
.*, –>* |
Pointer-to-member (objects), Pointer-to-member (pointers) |
Left to right |
|
*, /, % |
Multiplication, Division, Modulus |
Left to right |
|
+, – |
Addition, Subtraction |
Left to right |
|
<<, >> |
Left shift Right shift |
Left to right |
|
<, >, <=, >= |
Less than, Greater than, Less than or equal to, Greater than or equal to |
Left to right |
|
==, != |
Equality, Inequality |
Left to right |
|
& |
Bitwise AND |
Left to right |
|
^ |
Bitwise exclusive OR |
Left to right |
|
| |
Bitwise inclusive OR |
Left to right |
|
&& |
Logical AND |
Left to right |
|
|| |
Logical OR |
Left to right |
|
e1 ? e2 : e3 |
Conditional |
Right to left |
|
=*, =/, =%, =+, =–, =<<, =>>, =&, =|, =^, = |
Assignment Multiplication, assignment Division, assignment Modulus, assignment Addition, assignment Subtraction, assignment Left-shift, assignment Right-shift, assignment Bitwise AND, assignment Bitwise inclusive OR, assignment Bitwise exclusive OR, assignment |
Right to left |
|
throw expr |
throw expression |
Right to left |
|
, |
Comma |
Left to right |
|
Table 2 |
||
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)
Example:
static bool operator==(const Operation^, const Operation^);
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 operatorx, 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+=.
Overloadable Operators
|
Operator |
Name |
Type |
|
, |
Comma |
Binary |
|
! |
Logical NOT |
Unary |
|
!= |
Inequality |
Binary |
|
% |
Modulus |
Binary |
|
%= |
Modulus assignment |
Binary |
|
& |
Bitwise AND |
Binary |
|
& |
Address-of |
Unary |
|
&& |
Logical AND |
Binary |
|
&= |
Bitwise AND assignment |
Binary |
|
( ) |
Function call |
— |
|
* |
Multiplication |
Binary |
|
* |
Pointer dereference |
Unary |
|
*= |
Multiplication assignment |
Binary |
|
+ |
Addition |
Binary |
|
+ |
Unary Plus |
Unary |
|
++ |
Increment1 |
Unary |
|
+= |
Addition assignment |
Binary |
|
– |
Subtraction |
Binary |
|
– |
Unary negation |
Unary |
|
–– |
Decrement1 |
Unary |
|
–= |
Subtraction assignment |
Binary |
|
–> |
Member selection |
Binary |
|
–>* |
Pointer-to-member selection |
Binary |
|
/ |
Division |
Binary |
|
/= |
Division assignment |
Binary |
|
< |
Less than |
Binary |
|
<< |
Left shift |
Binary |
|
<<= |
Left shift assignment |
Binary |
|
<= |
Less than or equal to |
Binary |
|
= |
Assignment |
Binary |
|
== |
Equality |
Binary |
|
> |
Greater than |
Binary |
|
>= |
Greater than or equal to |
Binary |
|
>> |
Right shift |
Binary |
|
>>= |
Right shift assignment |
Binary |
|
[ ] |
Array subscript |
— |
|
^ |
Exclusive OR |
Binary |
|
^= |
Exclusive OR assignment |
Binary |
|
| |
Bitwise inclusive OR |
Binary |
|
|= |
Bitwise inclusive OR assignment |
Binary |
|
|| |
Logical OR |
Binary |
|
~ |
One's complement |
Unary |
|
delete |
Delete |
— |
|
new |
New |
— |
|
conversion operators |
conversion operators |
Unary |
|
Table 3 |
||
Note: Two versions of the unary increment and decrement operators exist: preincrement and postincrement.
Non Overloadable Operators
The operators shown in the following table cannot be overloaded. The table includes the preprocessor symbols # and ##.
|
Operator |
Name |
|
. |
Member selection |
|
.* |
Pointer-to-member selection |
|
:: |
Scope resolution |
|
? : |
Conditional |
|
# |
Preprocessor convert to string |
|
## |
Preprocessor concatenate |
|
Table 4 |
|
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.
Example
The following traditional C++ code example overloads the + operator to add two complex numbers and returns the result.
// traditional C++ operator overloading
// compile with: /clr
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
struct Complex
{
Complex(double r, double i) : re(r), im(i) {}
// Overloaded the addition...
Complex operator+(Complex &other);
void Display()
{
cout<<"Real part = "<<re<<", "<<"Imaginary part = "<<im<< endl;
}
private:
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);
cout<<"a: ";
a.Display();
Complex b = Complex(5.6, 7.8);
cout<<"b: ";
b.Display();
Complex c = Complex(0.0, 0.0);
cout<<"c: ";
c.Display();
cout<<endl;
c = a + b;
cout<<"c = a + b: ";
c.Display();
}
Output:

Overloaded Operators
Operator overloading has changed significantly from Managed Extensions for C++ to Visual C++ 2005. Perhaps the most striking aspect of Managed Extensions is its support for operator overloading or rather, its effective absence. Within the declaration of a reference type, for example, rather than using the native operator+ syntax, one has to explicitly write out the underlying internal name of the operator, in this case, op_Addition. More onerous, however, is the fact that the invocation of an operator has to be explicitly invoked through that name, thus precluding the two primary benefits of operator overloading:
For example:
// oper