Espacios de nombres
Variantes
Acciones

Copy constructors

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.
default constructor
copiar constructor
mover constructor (C++11)
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
 
Un constructor de copia T clase es un constructor no-plantilla cuyo primer parámetro es T&, const T&, volatile T& o const volatile T&, y, o bien no existen otros parámetros, o el resto de los parámetros tienen valores por defecto. Un tipo con un constructor de copia pública es CopyConstructible .
Original:
A copy constructor of class T is a non-template constructor whose first parameter is T&, const T&, volatile T&, or const volatile T&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public copy constructor is CopyConstructible.
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

class_name ( const class_name & ) (1)
class_name ( const class_name & ) = default; (1)
class_name ( const class_name & ) = delete; (1)

[editar] Explicación

# Declaración típica de un constructor de copia
Original:
# Typical declaration of a copy constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Obligar a un constructor de copias que serán generados por el compilador
Original:
# Forcing a copy constructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Evitar el constructor predeterminado implícito
Original:
# Avoiding implicit default constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
El constructor de copia se llama siempre que un objeto se inicializa desde otro objeto del mismo tipo, que incluye
Original:
The copy constructor is called whenever an object is initialized from another object of the same type, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • inicialización, T a = b; o T a(b);, donde b es de tipo T
    Original:
    initialization, T a = b; or T a(b);, where b is of type T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • que pasa función argumento: f(a);, donde a es de tipo T y f es void f(T t)
    Original:
    function argument passing: f(a);, where a is of type T and f is void f(T t)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • retorno de la función: return a; dentro de una función, como T f(), donde a es de T tipo, que no tiene constructor movimiento .
    Original:
    function return: return a; inside a function such as T f(), where a is of type T, which has no move constructor.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[editar] Implícitamente, declarado constructor de copia

Si no definidos por el usuario constructores de copia se proporcionan para un tipo de clase (struct, class o union), el compilador siempre declarar un constructor de copia como miembro inline public de su clase. Este constructor copia implícitamente declarada tiene la forma T::T(const T&) si todo lo siguiente es cierto:
Original:
If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as an inline public member of its class. This implicitly-declared copy constructor has the form T::T(const T&) if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • todas las bases directas y virtuales de T tener constructores de copia con referencias a const const o volátil como sus primeros parámetros
    Original:
    all direct and virtual bases of T have copy constructors with references to const or to const volatile as their first parameters
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • todos los miembros no estáticos de T tener constructores de copia con referencias a const const o volátil como sus primeros parámetros
    Original:
    all non-static members of T have copy constructors with references to const or to const volatile as their first parameters
    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, el constructor de copia implícitamente declarado es T::T(T&). (Tenga en cuenta que debido a estas reglas, el constructor de copia implícitamente declarada no puede obligar a un volátil argumento valor-I)
Original:
Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Una clase puede tener varios constructores de copia, por ejemplo, tanto T::T(const T&) y T::T(T&). Si algunos constructores de copia definidos por el usuario están presentes, el usuario todavía puede forzar la generación del constructor de copia implícitamente declarado con la palabra default (desde C++11) .
Original:
A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default (desde C++11).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Suprimido implícitamente declarada constructor de copia

El constructor de copia implícitamente declaradas o cesación de pagos por T clase es (hasta C++11) undefined / define como borrado (desde C++11) en cualquiera de las siguientes situaciones:
Original:
The implicitly-declared or defaulted copy constructor for class T is undefined (hasta C++11) / defined as deleted (desde C++11) in any of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T tiene miembros no estáticos de datos que no se pueden copiar (se han borrado, inaccesible, o constructores ambiguas copia)
    Original:
    T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T tiene clase base directa o virtual que no se puede copiar (se ha suprimido, constructores de copia inaccesibles, o ambiguo)
    Original:
    T has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T tiene clase base directa o virtual con un destructor borrado o inaccesible
    Original:
    T has direct or virtual base class with a deleted or inaccessible destructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T tiene un constructor definido por el usuario o (desde C++11) movimiento mudanza operador de asignación
    Original:
    T has a user-defined move constructor or move assignment operator (desde C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T es un sindicato y tiene un miembro con variante no-trivial (desde C++11) constructor de copia
    Original:
    T is a union and has a variant member with non-trivial copy constructor (desde C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T tiene un miembro de datos de (desde C++11) rvalue tipo de referencia
    Original:
    T has a data member of rvalue reference type (desde C++11)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[editar] Constructor de copia Trivial

El constructor de copia implícitamente declarado para T clase es trivial si todo lo siguiente es cierto:
Original:
The implicitly-declared copy constructor for class T is trivial if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T no tiene funciones miembro virtuales
    Original:
    T has no virtual member functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T no tiene clases base virtuales
    Original:
    T has no virtual base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • El constructor de copia seleccionado para cada base directa de T es trivial
    Original:
    The copy constructor selected for every direct base of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • El constructor de copia seleccionado para cada tipo de clase no estática (o matriz de tipo de clase) memeber de T es trivial
    Original:
    The copy constructor selected for every non-static class type (or array of class type) memeber of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Un constructor de copia trivial es un constructor que crea una copia byte a byte de la representación del objeto de la discusión, y no realiza ninguna otra acción. Los objetos con constructores de copia triviales pueden ser copiados copiando sus representaciones de objeto de forma manual, por ejemplo, con std::memmove. Todos los tipos de datos compatibles con el lenguaje C (tipos POD) son trivialmente copiable .
Original:
A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors can be copied by copying their object representations manually, e.g. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Implícitamente definido constructor de copia

Si el constructor de copia implícitamente declarada no se elimina o trivial, está definido (es decir, un cuerpo de función es generado y compilado) por el compilador. Para los tipos de union, el constructor de copia implícitamente definido por copia la representación de objetos (como por std::memmove). Para los tipos de clase no sindicalizados (class y struct), el constructor realiza miembro de pleno derecho-sabio copia de las bases del objeto y de los miembros no estáticos, en su orden de inicialización, el uso directo de inicialización .
Original:
If the implicitly-declared copy constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
La generación del constructor de copia implícitamente definida es deprecated(desde C++11) si T tiene un destructor definido por el usuario o definida por el usuario operador de asignación de copia .
Original:
The generation of the implicitly-defined copy constructor is deprecated(desde C++11) if T has a user-defined destructor or user-defined copy assignment operator.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Notas

En muchas situaciones, los constructores de copia están optimizados, incluso si iban a producir efectos secundarios observables, copia elisión ver
Original:
In many situations, copy constructors are optimized out even if they would produce observable side-effects, see copia elisión
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Ejemplo

struct A {
    int n;
    A(int n=1) : n(n) {}
    A(const A& a) : n(a.n) {} // user-defined copy ctor
};
 
struct B : A {
    // implicit default ctor B::B()
    // implicit copy ctor B::B(const B&) 
};
 
struct C : B {
     C() : B() {}
 private:
     C(const C&); // non-copiable, C++98 style
};
 
int main()
{
    A a1(7);
    A a2(a1); // calls the copy ctor
    B b;
    B b2 = b;
    A a3 = b; // conversion to A& and copy ctor
    volatile A va(10);
    // A a4 = va; // compile error
 
    C c;
    // C c2 = c; // compile error
}