direct initialization
De cppreference.com
|
|
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. |
Inicializa un objeto de conjunto explícito de argumentos del constructor .
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.
Contenido |
[editar] Sintaxis
T object ( arg );
T object |
(1) | ||||||||
T object { arg };
T object |
(2) | (desde C++11) | |||||||
T ( other )
T |
(3) | ||||||||
static_cast< T >( other )
|
(4) | ||||||||
new T(args, ...)
|
(5) | ||||||||
Class::Class() : member(args, ...) {...
|
(6) | ||||||||
[arg](){...
|
(7) | (desde C++11) | |||||||
[editar] Explicación
Inicialización directa se realiza en las siguientes situaciones:
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)
inicialización con una lista no vacía entre paréntesis de expresiones
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 secuencia de lista de inicialización, si no constuctors lista de inicialización que se proporcionan y un constructor de coincidencias es accesible, y todas las conversiones implícitas necesarias no son estrechamiento .
Original:
during lista de inicialización 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)
inicialización de un prvalue temporal por reparto funcional o con una lista de expresiones entre paréntesis
Original:
initialization of a prvalue temporary by reparto funcional 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)
inicialización de un prvalue temporal por 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)
inicialización de un objeto con el tiempo de almacenamiento dinámico por una nueva expresión con un inicializador no-vacío
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)
inicialización de una base o un miembro no estático por inicializador lista constructor
Original:
initialization of a base or a non-static member by constructor inicializador 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)
inicialización de miembros de objetos de cierre de las variables capturadas por copia en un lambda-expresión
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.
Los efectos de la inicialización directa son:
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.
- Si
Tes un tipo de clase, los constructores deTse examinan y el mejor partido es seleccionado por resolución de sobrecarga. El constructor se llama entonces para inicializar el objeto .Original:IfTis a class type, the constructors ofTare 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.
- De lo contrario, si
Tes un tipo no-clase, conversiones estándar se utilizan, si es necesario, para convertir el valor de other a la versión cv-incondicional deT.Original:Otherwise, ifTis a non-class type, conversiones estándar 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.
[editar] Notas
Direct-inicialización es más permisiva que la copia de inicialización: copy-inicialización sólo tiene en cuenta no explícitas constructores y funciones definidas por el usuario de conversión, mientras que directa-inicialización considera todos los constructores y las secuencias de conversión implícitas .
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.
[editar] Ejemplo
#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'; }
Salida:
test aaaaaaaaaa 1 2

