copy 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 otro objeto
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.
Contenido |
[editar] Sintaxis
T object = other ;
|
(1) | ||||||||
f(other);
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
[editar] Explicación
Copia de inicialización se lleva a cabo en las siguientes situaciones:
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)
cuando una variable llamada (automático, estático, hilo o local-) se declara con el inicializador que consiste en un signo igual seguido de una expresión .
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)
cuando se pasa un argumento a una función por valor
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)
cuando regresaba de una función que devuelve por valor
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)
cuando detecta una excepción por valor
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)
como parte de inicialización de agregados, para inicializar cada elemento para el que se proporciona un inicializador
Original:
as part of inicialización de agregados, 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.
Los efectos de la inicialización de copia son:
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.
- Si
Tes un tipo de clase y el tipo de other se cv-incondicional versión deTo una clase derivada deT, los constructores deTse examinan y la mejor coincidencia es seleccionado por resolución de sobrecarga. El constructor se llama entonces para inicializar el objeto .Original:IfTis a class type and the type of other is cv-unqualified version ofTor a class derived fromT, 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.
- Si
Tes un tipo de clase, y el tipo de other es diferente, o siTno es de tipo de clase, pero el tipo de other es un tipo de clase, definido por el usuario secuencias de conversión que puede convertir el tipo de other aTse examinó y mejor se selecciona por medio de resolución de sobrecarga. El resultado de la conversión, que es un prvalue temporal del tipo de destino, entonces se utiliza para dirigir a inicializar el objeto. El último paso es generalmente eliminada y el resultado de la función de conversión está construida directamente en la memoria asignada para el objeto de destino, pero el constructor de copia se requiere para ser accesibles incluso cuando no se utiliza .Original:IfTis a class type, and the type of other is different, or ifTis non-class type, but the type of other is a class type, definido por el usuario secuencias de conversión that can convert from the type of other toTare 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 dirigir a inicializar the object. The last step is usually eliminada 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.
- En caso contrario (si ninguno
Tni el tipo de other son los tipos de clase), conversiones estándar se utilizan, si es necesario, para convertir el valor de other a la versión cv-incondicional deT.Original:Otherwise (if neitherTnor the type of other are class types), 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
Copy-inicialización es menos permisiva que directa-inicialización: copy-inicialización sólo tiene en cuenta no explícitas constructores y funciones definidas por el usuario de conversión .
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.
Si other es una expresión rvalue, mover constructor serán seleccionados por la resolución de sobrecarga y llamó durante la copia de inicialización .
Original:
If other is an rvalue expression, mover constructor 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.
El signo de igualdad,
=, en copia-inicialización de una variable con nombre no está relacionado con el operador de asignación. Sobrecargas del operador de asignación no tendrá ningún efecto sobre la copia de inicialización .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.
[editar] Ejemplo
#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 }

