Logical operators
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Returns the result of a boolean operation.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
negation | not a
|
Yes | bool T::operator!() const; | bool operator!(const T &a); |
AND | a and b
|
Yes | bool T::operator&&(const T2 &b) const; | bool operator&&(const T &a, const T2 &b); |
inclusive OR | a or b
|
Yes | bool T::operator||(const T2 &b) const; | bool operator||(const T &a, const T2 &b); |
|
Inhaltsverzeichnis |
[Bearbeiten] Erklärung
The logical operators apply logic functions (NOT, AND, and inclusive OR) to boolean arguments (or types contextually-convertible to bool), with a boolean result. Unlike the bitwise logic operators, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first.
[Bearbeiten] Builtin operators
The following built-in function signatures participate in overload resolution:
bool operator!(bool) |
||
bool operator&&(bool, bool) |
||
bool operator||(bool, bool) |
||
If the operand is not bool, it is converted to bool using contextual conversion to bool: it is only well-formed if the declaration bool t(arg)
is well-formed, for some invented temporary t
.
For the built-in logical NOT operator, the result is true if the operand is false. Otherwise, the result is false.
For the built-in logical AND operator, the result is true if both operands are true. Otherwise, the result is false. If the first operand is false, the second operand is not evaluated.
For the built-in logical OR operator, the result is true if either the first or the second operand (or both) is true. If the firstoperand is true, the second operand is not evaluated.
[Bearbeiten] Results
a | true | false |
---|---|---|
!a | false | true |
and | a | ||
---|---|---|---|
true | false | ||
b | true | true | false |
false | false | false |
or | a | ||
---|---|---|---|
true | false | ||
b | true | true | true |
false | true | false |
[Bearbeiten] Beispiel
#include <iostream> #include <string> int main() { int n = 2; int* p = &n; // pointers are convertible to bool if( p && *p == 2 // "*p" is safe to use after "p &&" || !p && n != 2 ) // || has lower precedence than && std::cout << "true\n"; // streams are also convertible to bool std::cout << "Enter 'quit' to quit.\n"; for(std::string line; std::cout << "> " && std::getline(std::cin, line) && line != "quit"; ) ; }
Output:
true Enter 'quit' to quit. > test > quit
[Bearbeiten] Standard-Bibliothek
Because the short-circuiting properties of operator&&
and operator||
do not apply to overloads, and because types with boolean semantics are uncommon, only two standard library classes overload these operators:
gilt eine unäre arithmetische Operator für jedes Element des valarray Original: applies a unary arithmetic operator to each element of the valarray The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion of std::valarray )
| |
gilt Binäroperatoren auf jedes Element von zwei valarrays oder ein valarray und einem Wert Original: applies binary operators to each element of two valarrays, or a valarray and a value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Funktions-Template) | |
prüft, ob ein Fehler aufgetreten ist (Synonym fail()) Original: checks if an error has occurred (synonym of fail()) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion of std::basic_ios )
|
[Bearbeiten] Siehe auch
Common operators | ||||||
---|---|---|---|---|---|---|
Zuweisungen | incrementNJdecrement | Arithmetik | logisch | Vergleich | memberNJaccess | andererseits |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast wandelt einem Typ in einen anderen kompatiblen Typ
Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast wandelt virtuellen Basisklasse abgeleitet class
Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast wandelt Typ kompatiblen Typ mit unterschiedlichen cv qualifiers
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast wandelt Typ inkompatibel type
Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new ordnet memory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete freigibt memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof fragt die Größe eines type
Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof... fragt die Größe eines Parameter Pack (seit C++11)
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeid fragt die Typinformationen eines type
Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept prüft, ob ein Ausdruck eine Ausnahme (seit C++11)
werfen kann Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignof Abfragen Ausrichtungsanforderungen eines Typs (seit C++11)
Original: alignof queries alignment requirements of a type (seit C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |