Espaces de noms
Variantes
Affichages
Actions

Assignment operators

De cppreference.com
< cpp‎ | language

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 
Les opérateurs d'affectation modifier la valeur de l'objet .
Original:
Assignment operators modify the value of the object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operator name Syntax Over​load​able Prototype examples (for class T)
Inside class definition Outside class definition
basic assignment a = b Yes T& T::operator =(const T2& b); N/A
move assignment (C++11) a = rvalue Yes T& T::operator =(T2&& b); N/A
addition assignment a += b Yes T& T::operator +=(const T2& b); T& operator +=(T& a, const T2& b);
subtraction assignment a -= b Yes T& T::operator -=(const T2& b); T& operator -=(T& a, const T2& b);
multiplication assignment a *= b Yes T& T::operator *=(const T2& b); T& operator *=(T& a, const T2& b);
division assignment a /= b Yes T& T::operator /=(const T2& b); T& operator /=(T& a, const T2& b);
modulo assignment a %= b Yes T& T::operator %=(const T2& b); T& operator %=(T& a, const T2& b);
bitwise AND assignment a &= b Yes T& T::operator &=(const T2& b); T& operator &=(T& a, const T2& b);
bitwise OR assignment a |= b Yes T& T::operator |=(const T2& b); T& operator |=(T& a, const T2& b);
bitwise XOR assignment a ^= b Yes T& T::operator ^=(const T2& b); T& operator ^=(T& a, const T2& b);
bitwise left shift assignment a <<= b Yes T& T::operator <<=(const T2& b); T& operator <<=(T& a, const T2& b);
bitwise right shift assignment a >>= b Yes T& T::operator >>=(const T2& b); T& operator >>=(T& a, const T2& b);
'Note'
Original:
Notes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Tous intégrés dans l'affectation des opérateurs retourner *this, et la plupart définies par l'utilisateur surcharges également revenir *this de sorte que les opérateurs définis par l'utilisateur peut être utilisé de la même manière que le haut-ins. Cependant, définie par l'utilisateur surcharge d'opérateur, n'importe quel type peut être utilisé comme type de retour (y compris void) .
    Original:
    All built-in assignment operators return *this, and most user-defined overloads also return *this so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2 peuvent être de tout type, y compris T
    Original:
    T2 can be any type including T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Sommaire

[modifier] Explication

D'affectation de copie' opérateur remplace le contenu de la a objet avec une copie du contenu de b (b n'est modifié). Pour les types de classe, il s'agit d'une fonction membre particulière, décrite dans copier opérateur d'affectation .
Original:
copy assignment operator replaces the contents of the object a with a copy of the contents of b (b is no modified). For class types, this is a special member function, described in copier opérateur d'affectation.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Affectation mouvement' opérateur remplace le contenu de la a objet avec le contenu de b, en évitant, si possible, la copie (b peut être modifié). Pour les types de classe, il s'agit d'une fonction membre particulière, décrite dans déplacer opérateur d'affectation. (depuis C++11)
Original:
move assignment operator replaces the contents of the object a with the contents of b, avoiding copying if possible (b may be modified). For class types, this is a special member function, described in déplacer opérateur d'affectation. (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Pour les non-classe types, de copie et d'affectation mouvement sont indiscernables et sont appelés affectation directe .
Original:
For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
' Opérateurs composés d'affectation remplacer le contenu le contenu de l'objet a avec le résultat d'une opération binaire entre la valeur précédente de a et la valeur de b .
Original:
compound assignment operators replace the contents the contents of the object a with the result of a binary operation between the previous value of a and the value of b.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier] Builtin affectation directe

Pour chaque type de T, les signatures de fonction suivants participent à la résolution de surcharge:
Original:
For every type T, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
T*& operator=(T*&, T*);
T*volatile & operator=(T*volatile &, T*);
Pour chaque énumération ou un pointeur vers T type de membre, éventuellement volatile qualifié, la signature de la fonction suivante participe à la résolution de surcharge:
Original:
For every enumeration or pointer to member type T, optionally volatile-qualified, the following function signature participates in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
T& operator=(T&, T );
Pour chaque paire A1 et A2, où A1 est un type arithmétique (éventuellement qualifié volatile) et A2 est un type arithmétique promotion, la signature de la fonction suivante participe à la résolution de surcharge:
Original:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
A1& operator=(A1&, A2);
Pour les expressions E1 de toute T type scalaire, les formes suivantes supplémentaires de l'expression d'assignation builtin sont autorisés:
Original:
For expressions E1 of any scalar type T, the following additional forms of the builtin assignment expression are allowed:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
E1 = {}
(depuis C++11)
E1 = {E2}
(depuis C++11)
Remarque: le ci-dessus comprend tous les types de non-classe, sauf les types de référence, les types tableau, types de fonction et l'void type, qui ne sont pas directement imputables .
Original:
Note: the above includes all non-class types except reference types, array types, function types, and the type void, which are not directly assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
L'opérateur d'affectation directe s'attend à une lvalue modifiable que son opérande de gauche et retourne une lvalue identifier l'opérande de gauche après modification. Pour les non-classe les types, l'opérande de droite est implicitement convertie première à la cv-Type sans réserve de l'opérande de gauche, puis sa valeur est copiée dans l'objet identifié par l'opérande gauche .
Original:
The direct assignment operator expects a modifiable lvalue as its left operand and returns an lvalue identifying the left operand after modification. For non-class types, the right operand is first implicitement convertie to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Lorsque l'opérande de gauche est un type référence, l'opérateur d'affectation s'applique à l'objet visé à .
Original:
When the left operand is a reference type, the assignment operator applies to the referred-to object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si la gauche et les opérandes droit identifier les objets qui se chevauchent, le comportement est indéfini (à moins que le chevauchement est exact et le type est le même)
Original:
If the left and the right operands identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si l'opérande de droite est un contreventés-init-list
Original:
If the right operand is a braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • E1 = {} l'expression est équivalente à E1 = T(), où T est le type de E1 .
    Original:
    the expression E1 = {} is equivalent to E1 = T(), where T is the type of E1.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • E1 = {E2} l'expression est équivalente à E1 = T(E2), où T est le type de E1, sauf que les conversions restrictives implicites sont interdits .
    Original:
    the expression E1 = {E2} is equivalent to E1 = T(E2), where T is the type of E1, except that narrowing implicit conversions are prohibited.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Pour les types de classe, cette syntaxe génère un appel à l'opérateur d'affectation avec std::initializer_list comme argument, suivant les règles de liste d'initialisation
Original:
For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of liste d'initialisation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier] Exemple

#include <iostream>
int main()
{
    int n = 0;  // not an assignment
    n = 1;      // direct asignment
    std::cout << n << ' ';
    n = {};     // zero-initialization, then assignment
    std::cout << n << ' ';
    n = 'a';    // integral promotion, then assignment
    std::cout << n << ' ';
    n = {'b'};   // explicit cast, then assignment
    std::cout << n << ' ';
    n = 1.0;    // floating-point conversion, then assignment
    std::cout << n << ' ';
//    n = {1.0}; // compiler error (narrowing conversion)
 
    int& r = n;  // not an assignment
    int* p;
 
    r = 2;       // assignment through reference
    std::cout << n << ' ';
    p = &n;      // direct assignment
    p = nullptr; // null-pointer conversion, then assignment 
}

Résultat :

1 0 97 98 1 2

[modifier] Affectation composée intégré

Pour chaque paire A1 et A2, où A1 est un type arithmétique (éventuellement qualifié volatile) et A2 est un type arithmétique promu, les signatures de fonction suivants participent à la résolution de surcharge:
Original:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
A1& operator*=(A1&, A2);
A1& operator/=(A1&, A2);
A1& operator+=(A1&, A2);
A1& operator-=(A1&, A2);
Pour chaque paire I1 et I2, où I1 est un type intégral (en option volatile qualifié) et I2 est un type de promotion intégrale, les signatures de fonction suivants participent à la résolution de surcharge:
Original:
For every pair I1 and I2, where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
I1& operator%=(I1&, I2);
I1& operator<<=(I1&, I2);
I1& operator>>=(I1&, I2);
I1& operator&=(I1&, I2);
I1& operator^=(I1&, I2);
I1& operator|=(I1&, I2);
Pour chaque type d'objet T option cv-qualifié, les signatures de fonction suivants participent à la résolution de surcharge:
Original:
For every optionally cv-qualified object type T, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
T*& operator+=(T*&, std::ptrdiff_t);
T*& operator-=(T*&, std::ptrdiff_t);
T*volatile & operator+=(T*volatile &, std::ptrdiff_t);
T*volatile & operator-=(T*volatile &, std::ptrdiff_t);
{{{1}}}
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier] Exemple

[modifier] Voir aussi

La priorité des opérateurs

Opérateurs ordinaires
affectation incrémentation
décrémentation
arithmétique logique comparaison accès aux membre autre

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

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)