Namespace
Varianti

copy initialization

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controllo del flusso
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dichiarazioni esecuzione condizionale
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterazione dichiarazioni
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Vai dichiarazioni
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
esplicito (C++11)
statico
Funzioni membro speciali
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
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.

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.
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.
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.
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.
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.
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.
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.
  • Se T è un tipo di classe e il tipo di other viene cv-unqualified versione di T o una classe derivata da T, i costruttori di T vengono esaminate e la corrispondenza migliore è selezionato per la risoluzione di sovraccarico. Il costruttore viene chiamato per inizializzare l'oggetto.
    Original:
    If T is a class type and the type of other is cv-unqualified version of T or a class derived from T, the constructors of T 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 a T 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:
    If T is a class type, and the type of other is different, or if T 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 to T 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 di T.
    Original:
    Otherwise (if neither T 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 of T.
    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.
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.
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.

[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
}


[modifica] Vedi anche