Assignment 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. |
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 | |||
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); |
|
Índice |
[editar]Explicação
a
objecto com uma cópia dos conteúdos do b
(b
é não modificada). Para os tipos de classe, esta é uma função de membro especial, descrita no copiar operador de atribuição.a
with a copy of the contents of b
(b
is no modified). For class types, this is a special member function, described in copiar operador de atribuição.You can help to correct and verify the translation. Click here for instructions.
a
objecto com o conteúdo de b
, evitando, se possível, a cópia (b
pode ser modificado). Para os tipos de classe, esta é uma função de membro especial, descrita no mover operador de atribuição. (desde C++11)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 mover operador de atribuição. (desde C++11)You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
a
objecto com o resultado de uma operação de binário entre o valor anterior de a
e o valor de b
.a
with the result of a binary operation between the previous value of a
and the value of b
.You can help to correct and verify the translation. Click here for instructions.
[editar]Builtin atribuição direta
T
, as assinaturas de função seguintes participar na resolução de sobrecarga:T
, the following function signatures participate in overload resolution:You can help to correct and verify the translation. Click here for instructions.
T*& operator=(T*&, T*); | ||
T*volatile& operator=(T*volatile&, T*); | ||
T
tipo de membro, opcionalmente volátil qualificado, a assinatura da função seguinte participa de resolução de sobrecarga:T
, optionally volatile-qualified, the following function signature participates in overload resolution:You can help to correct and verify the translation. Click here for instructions.
T& operator=(T&, T ); | ||
You can help to correct and verify the translation. Click here for instructions.
A1& operator=(A1&, A2); | ||
T
, as seguintes formas adicionais de expressão de atribuição builtin são permitidos:T
, the following additional forms of the builtin assignment expression are allowed:You can help to correct and verify the translation. Click here for instructions.
E1 ={} | (desde C++11) | |
E1 ={E2} | (desde C++11) | |
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- o E1 ={} expressão é equivalente a E1 = T(), onde
T
é o tipo deE1
.Original:the expression E1 ={} is equivalent to E1 = T(), whereT
is the type ofE1
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - o E1 ={E2} expressão é equivalente a E1 = T(E2), onde
T
é o tipo deE1
, excepto que as conversões de estreitamento implícitas são proibidos.Original:the expression E1 ={E2} is equivalent to E1 = T(E2), whereT
is the type ofE1
, 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.
You can help to correct and verify the translation. Click here for instructions.
[editar]Exemplo
#include <iostream>int main(){int n =0;// not an assignment n =1;// direct asignmentstd::cout<< n <<' '; n ={};// zero-initialization, then assignmentstd::cout<< n <<' '; n ='a';// integral promotion, then assignmentstd::cout<< n <<' '; n ={'b'};// explicit cast, then assignmentstd::cout<< n <<' '; n =1.0;// floating-point conversion, then assignmentstd::cout<< n <<' ';// n = {1.0}; // compiler error (narrowing conversion) int& r = n;// not an assignmentint* p; r =2;// assignment through referencestd::cout<< n <<' '; p =&n;// direct assignment p = nullptr;// null-pointer conversion, then assignment }
Saída:
1 0 97 98 1 2
[editar]Builtin atribuição composto
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); | ||
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); | ||
T
opcionalmente cv qualificado tipo de objeto, as assinaturas de função seguintes participar na resolução de sobrecarga:T
, the following function signatures participate in overload resolution: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); | ||
You can help to correct and verify the translation. Click here for instructions.
[editar]Exemplo
Esta seção está incompleta Motivo: sem exemplo |
[editar]Veja também
Operadores comuns | ||||||
---|---|---|---|---|---|---|
assinamento | incremento descremento | aritmético | lógico | comparação | acesso de membro | outros |
a = b | ++a | +a | !a | a == b | a[b] | a(...) |
Operadores Especiais | ||||||
static_cast converte um tipo a um outro tipo relacionado |