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. |
<metanoindex/>
You can help to correct and verify the translation. Click here for instructions.
| 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);
|
| ||||
Explication
bool), avec un résultat booléen. Contrairement à la opérateurs logiques bit à bit, ces opérateurs (dans leur formulaire intégré) n'évaluent pas le second opérande, si le résultat est connu après une évaluation de la première .bool), with a boolean result. Unlike the opérateurs logiques bit à bit, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first.You can help to correct and verify the translation. Click here for instructions.
Opérateurs intégrés
You can help to correct and verify the translation. Click here for instructions.
bool operator!(bool) |
||
bool operator&&(bool, bool) |
||
bool operator||(bool, bool) |
||
bool, il est converti en utilisant bool conversion contextuelle bool: ce n'est que bien formée si la déclaration bool t(arg) est bien formée, pour certains t inventé temporaire .bool, it is converted to bool using conversion contextuelle bool: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.You can help to correct and verify the translation. Click here for instructions.
true si l'opérande est false. Dans le cas contraire, le résultat est false .true if the operand is false. Otherwise, the result is false.You can help to correct and verify the translation. Click here for instructions.
true si les deux opérandes sont true. Dans le cas contraire, le résultat est false. Si le premier opérande est false, le second opérande n'est pas évalué .true if both operands are true. Otherwise, the result is false. If the first operand is false, the second operand is not evaluated.You can help to correct and verify the translation. Click here for instructions.
true si le premier ou le deuxième opérande (ou les deux) est true. Si le firstoperand est true, le second opérande n'est pas évalué .true if either the first or the second operand (or both) is true. If the firstoperand is true, the second operand is not evaluated.You can help to correct and verify the translation. Click here for instructions.
Résultats
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
| |
Exemple
#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"; )
;
}
Résultat :
true
Enter 'quit' to quit.
> test
> quit
Bibliothèque standard
operator&& et operator|| ne s'appliquent pas aux surcharges, et parce que les types booléens avec la sémantique sont rares, seulement deux classes de la bibliothèque standard surcharger ces opérateurs:operator&& and operator|| do not apply to overloads, and because types with boolean semantics are uncommon, only two standard library classes overload these operators:You can help to correct and verify the translation. Click here for instructions.
applique un opérateur arithmétique unaire pour chaque élément de la 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. (fonction membre publique de std::valarray)
| |
des opérateurs binaires s'applique à chaque élément de valarrays deux, ou un valarray et une valeur 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. (fonction générique) | |
vérifie si une erreur s'est produite (synonyme de 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. (fonction membre publique de std::basic_ios)
| |
Voir aussi
| Opérateurs ordinaires | ||||||
|---|---|---|---|---|---|---|
| affectation | incrémentation décrémentation |
arithmétique | logique | comparaison | accès aux membre | autre |
|
|
|
|
|
|
|
|
| Opérateurs spéciaux | ||||||
|
static_cast convertit un type dans un autre type compatible dynamic_cast convertit une classe de base virtuelle dans une classe dérivée const_cast convertit un type dans un type compatible avec des cv-qualifiers différents reinterpret_cast convertit un type dans un type incompatibles new allocation de la mémoire delete libère de la mémoire sizeof récupère la taille d'un type sizeof... récupère la taille d'un paquet de paramètres (depuis C++11) typeid récupère les informations de type d'un type noexcept vérifie si une expression peut lancer une exception (depuis C++11) alignof récupère les conditions d'alignement d'un type (depuis C++11) | ||||||