Espacios de nombres
Variantes
Acciones

copy initialization

De cppreference.com
< cpp‎ | language

 
 
Lenguaje C++
Los temas generales
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.
Control de flujo
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.
Sentencias condicionales ejecución
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.
Instrucciones de iteración
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.
Ir declaraciones
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.
Funciones
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaración de la función
lambda declaración de la función
función de plantilla
especificador inline
especificaciones de excepción (obsoleto)
noexcept especificador (C++11)
Excepciones
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los espacios de nombres
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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)
Los especificadores
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 especificadores
almacenamiento duración especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialización
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los literales
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expresiones
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representaciones alternativas
Utilidades
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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
alias de tipo de declaración (C++11)
atributos (C++11)
Lanza
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversiones implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-cast estilo y funcional
De asignación de 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.
Clases
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Clase propiedades específicas de la función
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.
Funciones especiales miembro
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.
Plantillas
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
clase de plantilla
función de plantilla
plantilla de especialización
paquetes de parámetros (C++11)
Varios
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaje en línea
 
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.

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.
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.
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.
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.
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.
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.
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.
  • Si T es un tipo de clase y el tipo de other se cv-incondicional versión de T o una clase derivada de T, los constructores de T se examinan y la mejor coincidencia es seleccionado por resolución de sobrecarga. El constructor se llama entonces para inicializar el objeto .
    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.
  • Si T es un tipo de clase, y el tipo de other es diferente, o si T no 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 a T se 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:
    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, definido por el usuario secuencias de conversión 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 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 T ni 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 de T .
    Original:
    Otherwise (if neither T nor 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 of T.
    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.
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.
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.

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


[editar] Ver también