Operatoren kann man auf mehrere Arten unterscheiden. Zum einen nach ihrer Position, also ob sie vor dem Operanden stehen (z.B. !false), dann spricht man von einem Prefix-Operator (der Operator !
steht vor dem Operanden ''false), der Operator kann hinter dem Operanden stehen (i++), was man als Postfix-Operator bezeichnet und zwischen mehreren Operanden (1 + 2), was als Infix-Operator gilt.
Einige Operatoren können in C++ überladen werden. Das bedeutet, dass man für eigene Datentypen (Klassen eigene Regeln definieren kann, wie diese z.B. addiert werden können.
C++ unterstützt folgende Operatoren:
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
---|---|---|---|---|---|---|---|---|
18 | :: | Namensraum | system | - | Pre/Infix | L-R | Nein | NameSpace::Class::Method |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
17 | ++ | Inkrement | arithmetisch | set | Postfix | L-R | Ja | i++ |
-- | Dekrement | arithmetisch | set | Postfix | L-R | Ja | i– | |
[] | Array-Zugriff | typbezogen | get | Infix | L-R | Ja | array[i] | |
() | Priorität | - | get | - | L-R | Nein | (1+2)*3 | |
. | Zugriff | typbezogen | get | Infix | L-R | Ja | Instance.Method() | |
-> | Deref + Zugriff | typbezogen | get | Infix | L-R | Ja | InstancePtr→Method() | |
typeid | Typindentifikation | typbezogen | get | prefix | L-R | Nein | typeid( Instance ) | |
() | Funktionsruf | typbezogen | get | Infix | L-R | Ja | Function() | |
() | Funktor | typbezogen | get | Infix | L-R | Ja | MyClass( int i ) : Member( a ) | |
: | Initialisierung | Kopie | set | Infix | L-R | Nein | MyFunctorClass( i ) | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
16 | + | Positiv | arithmetisch | get | Prefix | R-L | Ja | +1 |
- | Negativ | arithmetisch | get | Prefix | R-L | Ja | -1 | |
! | Nicht | logisch | get | Prefix | R-L | Ja | !true | |
~ | Komplement | bit | get | Prefix | R-L | Ja | ~0 | |
++ | Inkrement | arithmetisch | set | Prefix | R-L | Ja | ++i | |
-- | Dekrement | arithmetisch | set | Prefix | R-L | Ja | –i | |
& | Referenzieren | typbezogen | get | Prefix | R-L | Ja | &i | |
* | Dereferenzieren | typbezogen | get | Prefix | R-L | Ja | *iPtr | |
new | Speicher anfordern | system | sys | prefix | R-L | Nein | new int() | |
delete | Speicher freigeben | system | sys | prefix | R-L | Nein | delete iPtr | |
sizeof | Typgröße | typbezogen | get | prefix | R-L | Nein | sizeif( int ) | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
15 | .* | indirekter Zugriff | typbezogen | get | Infix | L-R | Nein | MyClass.*IndirectMember() |
->* | indirekt deref + Zugriff | typbezogen | get | Infix | L-R | Ja | MyClassPtr→*IndirectMember() | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
14 | * | Multiplikation | arithmetisch | get | Infix | L-R | Ja | 1 * 2 |
/ | Division | arithmetisch | get | Infix | L-R | Ja | 1 / 2 | |
% | Modulo | arithmetisch | get | Infix | L-R | Ja | 1 % 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
13 | + | Addition | arithmetisch | get | Infix | L-R | Ja | 1 + 2 |
- | Subtraktion | arithmetisch | get | Infix | L-R | Ja | 1 - 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
12 | << | Shift Links | bit | get | Infix | L-R | Ja | 1 « 2 |
>> | Shift Rechts | bit | get | Infix | L-R | Ja | 1 » 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
11 | > | größer | vergleichend | get | Infix | L-R | Ja | 1 > 2 |
>= | größer/gleich | vergleichend | get | Infix | L-R | Ja | 1 >= 2 | |
< | kleiner | vergleichend | get | Infix | L-R | Ja | 1 < 2 | |
<= | kleiner/gleich | vergleichend | get | Infix | L-R | Ja | 1 ⇐ 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
10 | == | gleich | vergleichend | get | Infix | L-R | Ja | 1 == 2 |
!= | ungleich | vergleichend | get | Infix | L-R | Ja | 1 != 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
9 | & | Und | bit | get | Infix | L-R | Ja | 1 & 2 |
8 | ^ | Exklusives Oder | bit | get | Infix | L-R | Ja | 1 ^ 2 |
7 | | | Oder | bit | get | Infix | L-R | Ja | 1 | 2 |
6 | && | Und | logisch | get | Infix | L-R | Ja | true && false |
5 | || | Oder | logisch | get | Infix | L-R | Ja | true || false |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
4 | = | Kopieren | Kopie | set | Infix | R-L | Ja | i = 1 |
+= | Addition | arithmetisch | set | Infix | R-L | Ja | 1 += 2 | |
-= | Subtraktion | arithmetisch | set | Infix | R-L | Ja | 1 -= 3 | |
*= | Multiplikation | arithmetisch | set | Infix | R-L | Ja | 1 *= 4 | |
/= | Division | arithmetisch | set | Infix | R-L | Ja | 1 /= 5 | |
%= | Rest | arithmetisch | set | Infix | R-L | Ja | 1 %= 7 | |
&= | Und | bit | set | Infix | R-L | Ja | 1 &= ~( 1 « 4 ) | |
|= | Oder | bit | set | Infix | R-L | Ja | 1 |= ( 1 « 4 ) | |
^= | Exklusives Oder | bit | set | Infix | R-L | Ja | 1 ^= ( 1 « 4 ) | |
<<= | Shift Links | bit | set | Infix | R-L | Ja | 1 «= 1 | |
>>= | Shift Rechts | bit | set | Infix | R-L | Ja | 1 »= 2 | |
Priorität | Operator | Bedeutung | Anwendung | Typ | Notation | Assoziativität | überladbar | Beispiel |
3 | ?: | Bedingung | vergleichend | get | - | L-R | Nein | max = ( i > j ) ? i : j |
2 | throw | Exception | system | - | prefix | L-R | Nein | throw bad_alloc |
() | Cast | typbezogen | get | prefix | L-R | Ja | static_cast< int >( 1.0 ) | |
1 | , | Aufzählung | system | get | Infix | L-R | Nein | 1, 2, 3 |