copy initialization
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Inizializza un oggetto da un altro oggetto
Original:
Initializes an object from another object
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.
Indice |
[modifica] Sintassi
T object = other ;
|
(1) | ||||||||
f( other) ;
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
[modifica] Spiegazione
Copia di inizializzazione viene eseguita nei seguenti casi:
Original:
Copy initialization is performed in the following situations:
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.
1)
quando una variabile di nome (automatico, statico, o filo-locale) viene dichiarato con la inizializzatore costituito da un segno di uguale seguito da una espressione.
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
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.
2)
quando si passa un argomento a una funzione per valore
Original:
when passing an argument to a function by value
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.
3)
al ritorno da una funzione che restituisce per valore
Original:
when returning from a function that returns by value
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.
4)
quando cattura un'eccezione per valore
Original:
when catching an exception by value
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.
5)
come parte di inizializzazioni di aggregazione, per inizializzare ogni elemento per il quale è previsto un inizializzatore
Original:
as part of inizializzazioni di aggregazione, to initialize each element for which an initializer is provided
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.
Gli effetti di inizializzazione copia sono:
Original:
The effects of copy initialization are:
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.
- Se
T
è un tipo di classe e il tipo di other viene cv-unqualified versione diT
o una classe derivata daT
, i costruttori diT
vengono esaminate e la corrispondenza migliore è selezionato per la risoluzione di sovraccarico. Il costruttore viene chiamato per inizializzare l'oggetto.Original:IfT
is a class type and the type of other is cv-unqualified version ofT
or a class derived fromT
, the constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se
T
è un tipo di classe, e il tipo di other è diversa, o se non èT
classe tipo, ma il tipo di other è un tipo di classe, definito dall'utente sequenze di conversione che può convertire dal tipo di other aT
vengono esaminati e la migliore uno viene selezionato attraverso la risoluzione di sovraccarico. Il risultato della conversione, che è un prvalue temporanea del tipo di destinazione, viene quindi utilizzata per diretta-inizializzare dell'oggetto. L'ultimo passo è di solito eliminato e il risultato della funzione di conversione è costruito direttamente nella memoria allocata per l'oggetto bersaglio, ma il costruttore di copia deve essere accessibile anche se non è usato.Original:IfT
is a class type, and the type of other is different, or ifT
is non-class type, but the type of other is a class type, definito dall'utente sequenze di conversione that can convert from the type of other toT
are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to diretta-inizializzare the object. The last step is usually eliminato and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Altrimenti (se né
T
né il tipo di other sono tipi di classe), conversioni standard sono utilizzati, se necessario, per convertire il valore di other al CV-unqualified versione diT
.Original:Otherwise (if neitherT
nor the type of other are class types), conversioni standard are used, if necessary, to convert the value of other to the cv-unqualified version ofT
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Note
Copia-di inizializzazione è meno permissivo che diretto-inizializzazione: copia-inizializzazione prende in considerazione solo non espliciti costruttori e funzioni definite dall'utente di conversione.
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
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.
Se other è un'espressione rvalue, spostare costruttore saranno selezionati dalla risoluzione di sovraccarico e chiamato durante la copia, l'inizializzazione.
Original:
If other is an rvalue expression, spostare costruttore will be selected by overload resolution and called during copy-initialization.
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.
Il segno di uguale,
=
, in copia-inizializzazione di una variabile di nome non è legato al l'operatore di assegnazione. Overload dell'operatore di assegnazione non hanno effetto sulla copia-inizializzazione.Original:
The equals sign,
=
, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.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.
[modifica] Esempio
#include <string> #include <utility> #include <memory> int main() { std::string s = "test"; // OK: constructor is non-explicit std::string s2 = std::move(s); // this copy-initialization performs a move // std::unique_ptr<int> p = new int(1); // error: constructor is explicit std::unique_ptr<int> p(new int(1)); // OK: direct-initialization int n = 3.14; // floating-integral conversion const int b = n; // const doesn't matter int c = b; // ...either way }