====== Operatoren ====== 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. ===== Operatorüberladung ===== Einige Operatoren können in C++ überladen werden. Das bedeutet, dass man für eigene Datentypen ([[cpp:class:start|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 | [[postfix|Ja]] | i++ | | | -- | Dekrement | arithmetisch | set | Postfix | L-R | [[postfix|Ja]] | i-- | | | [] | Array-Zugriff | typbezogen | get | Infix | L-R | [[indexop|Ja]] | array[i] | | | () | Priorität | - | get | - | L-R | Nein | (1+2)*3 | | | . | Zugriff | typbezogen | get | Infix | L-R | [[infix|Ja]] | Instance.Method() | | | -> | Deref + Zugriff | typbezogen | get | Infix | L-R | [[infix|Ja]] | InstancePtr->Method() | | | typeid | Typindentifikation | typbezogen | get | prefix | L-R | Nein | typeid( Instance ) | | | () | Funktionsruf | typbezogen | get | Infix | L-R | [[cpp:overload:start|Ja]] | Function() | | | () | Funktor | typbezogen | get | Infix | L-R | [[functor|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 | [[prefix|Ja]] | +1 | | | - | Negativ | arithmetisch | get | Prefix | R-L | [[prefix|Ja]] | -1 | | | ! | Nicht | logisch | get | Prefix | R-L | [[prefix|Ja]] | !true | | | ~ | Komplement | bit | get | Prefix | R-L | [[prefix|Ja]] | ~0 | | | ++ | Inkrement | arithmetisch | set | Prefix | R-L | [[prefix|Ja]] | ++i | | | -- | Dekrement | arithmetisch | set | Prefix | R-L | [[prefix|Ja]] | --i | | | & | Referenzieren | typbezogen | get | Prefix | R-L | [[prefix|Ja]] | &i | | | * | Dereferenzieren | typbezogen | get | Prefix | R-L | [[prefix|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 | [[infix|Ja]] | MyClassPtr->*IndirectMember() | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 14 | * | Multiplikation | arithmetisch | get | Infix | L-R | [[infix|Ja]] | 1 * 2 | | | / | Division | arithmetisch | get | Infix | L-R | [[infix|Ja]] | 1 / 2 | | | % | Modulo | arithmetisch | get | Infix | L-R | [[infix|Ja]] | 1 % 2 | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 13 | + | Addition | arithmetisch | get | Infix | L-R | [[infix|Ja]] | 1 + 2 | | | - | Subtraktion | arithmetisch | get | Infix | L-R | [[infix|Ja]] | 1 - 2 | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 12 | << | Shift Links | bit | get | Infix | L-R | [[infix|Ja]] | 1 << 2 | | | >> | Shift Rechts | bit | get | Infix | L-R | [[infix|Ja]] | 1 >> 2 | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 11 | > | größer | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 > 2 | | | >= | größer/gleich | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 >= 2 | | | < | kleiner | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 < 2 | | | <= | kleiner/gleich | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 <= 2 | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 10 | == | gleich | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 == 2 | | | != | ungleich | vergleichend | get | Infix | L-R | [[infix|Ja]] | 1 != 2 | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 9 | & | Und | bit | get | Infix | L-R | [[infix|Ja]] | 1 & 2 | | 8 | ^ | Exklusives Oder | bit | get | Infix | L-R | [[infix|Ja]] | 1 ^ 2 | | 7 | | | Oder | bit | get | Infix | L-R | [[infix|Ja]] | 1 | 2 | | 6 | && | Und | logisch | get | Infix | L-R | [[infix|Ja]] | true && false | | 5 | || | Oder | logisch | get | Infix | L-R | [[infix|Ja]] | true || false | ^Priorität ^Operator ^Bedeutung ^ Anwendung ^Typ ^Notation ^ Assoziativität ^ überladbar ^Beispiel ^ | 4 | = | Kopieren | Kopie | set | Infix | R-L | [[infix|Ja]] | i = 1 | | | += | Addition | arithmetisch | set | Infix | R-L | [[infix|Ja]] | 1 += 2 | | | -= | Subtraktion | arithmetisch | set | Infix | R-L | [[infix|Ja]] | 1 -= 3 | | | *= | Multiplikation | arithmetisch | set | Infix | R-L | [[infix|Ja]] | 1 *= 4 | | | /= | Division | arithmetisch | set | Infix | R-L | [[infix|Ja]] | 1 /= 5 | | | %= | Rest | arithmetisch | set | Infix | R-L | [[infix|Ja]] | 1 %= 7 | | | &= | Und | bit | set | Infix | R-L | [[infix|Ja]] | 1 &= ~( 1 << 4 ) | | | |= | Oder | bit | set | Infix | R-L | [[infix|Ja]] | 1 |= ( 1 << 4 ) | | | ^= | Exklusives Oder | bit | set | Infix | R-L | [[infix|Ja]] | 1 ^= ( 1 << 4 ) | | | <<= | Shift Links | bit | set | Infix | R-L | [[infix|Ja]] | 1 <<= 1 | | | >>= | Shift Rechts | bit | set | Infix | R-L | [[infix|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 | [[convert|Ja]] | static_cast< int >( 1.0 ) | | 1 | , | Aufzählung | system | get | Infix | L-R | Nein | 1, 2, 3 |