direct 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 insieme esplicito di argomenti del costruttore.
Original:
Initializes an object from explicit set of constructor arguments.
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 ( arg );
T object |
(1) | ||||||||
T object { arg };
T object |
(2) | (dal C++11) | |||||||
T ( other )
T |
(3) | ||||||||
static_cast< T >( other )
|
(4) | ||||||||
new T( args, ...)
|
(5) | ||||||||
Class:: Class() : member( args, ...) {...
|
(6) | ||||||||
[ arg](){...
|
(7) | (dal C++11) | |||||||
[modifica] Spiegazione
Inizializzazione diretta viene eseguita nei seguenti casi:
Original:
Direct 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)
inizializzazione con una lista non vuota di espressioni tra parentesi
Original:
initialization with a nonempty parenthesized list of expressions
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)
durante la sequenza list-inizializzazione, se non inizializzatore-list constuctors sono forniti e un costruttore corrispondenza è accessibile, e tutte le conversioni necessarie implicite non sono restringimento.
Original:
during list-inizializzazione sequence, if no initializer-list constuctors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
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)
inizializzazione di un prvalue temporaneo getto funzionale o con un elenco di espressioni tra parentesi
Original:
initialization of a prvalue temporary by getto funzionale or with a parenthesized expression list
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)
inizializzazione di un prvalue temporaneo da un expession static_cast
Original:
initialization of a prvalue temporary by a static_cast expession
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)
inizializzazione di un oggetto con durata di stoccaggio dinamico da una nuova espressione con un non-vuota inizializzatore
Original:
initialization of an object with dynamic storage duration by a new-expression with a non-empty initializer
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.
6)
inizializzazione di una base o di un membro non statico da inizializzatore lista costruttore
Original:
initialization of a base or a non-static member by constructor inizializzatore lista
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.
7)
inizializzazione dei membri di chiusura oggetto dalle variabili catturati da copia in una lambda-espressione
Original:
initialization of closure object members from the variables caught by copy in a lambda-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.
Gli effetti di inizializzazione diretta sono:
Original:
The effects of direct 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, i costruttori diT
vengono esaminati e la migliore partita è selezionata per risoluzione di sovraccarico. Il costruttore viene chiamato per inizializzare l'oggetto.Original:IfT
is a class type, 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.
- Altrimenti, se non è un
T
classe tipo, conversioni standard sono utilizzati, se necessario, per convertire il valore di other al CV-unqualified versione diT
.Original:Otherwise, ifT
is a non-class type, 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
Diretto di inizializzazione è più permissivo di copy-inizializzazione: copia-inizializzazione prende in considerazione solo non espliciti costruttori e funzioni definite dall'utente di conversione, mentre il diretto inizializzazione considera tutti i costruttori e le sequenze di conversione impliciti.
Original:
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
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 <iostream> #include <memory> struct Foo { int mem; explicit Foo(int n) : mem(n) {} }; int main() { std::string s1("test"); // constructor from const char* std::string s2(10, 'a'); std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed // std::unique_ptr<int> p = new int(1); // error: constructor is explicit Foo f(2); // f is direct-initialized: // constructor parameter n is copy-initialized from the rvalue 2 // f.mem is direct-initialized from the parameter n // Foo f2 = 2; // error: constructor is explicit std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem << '\n'; }
Output:
test aaaaaaaaaa 1 2