◐ Shell
clean mode source ↗

Copy constructors - cppreference.com

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.

Click here for the English version of this page

<metanoindex/>

Un costruttore di copia di T classe non è un modello di costruzione il cui primo parametro è T&, const T&, volatile T& o const volatile T&, e sia non ci sono altri parametri, o il resto dei parametri di tutti hanno valori di default. Un tipo con un costruttore di copia pubblico 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.

Sintassi

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

Spiegazione

# Dichiarazione tipica di un costruttore di 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.

# Forzare un costruttore di copia deve essere generato dal compilatore

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.

# Evitare costruttore predefinito implicito

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.

Il costruttore di copia viene chiamato quando un oggetto è inizializzato da un altro oggetto dello stesso tipo, che comprende

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.

  • inizializzazione, T a = b; o T a(b);, dove b è di 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.

  • Passaggio di argomenti funzione: f(a);, dove a è di tipo T e f è 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.

  • di ritorno della funzione: return a; all'interno di una funzione, ad esempio T f(), dove a è di T tipo, che non ha alcun costruttore mossa.

    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.

Implicitamente-ha dichiarato costruttore di copia

Se non definiti dall'utente costruttori di copia sono forniti per un tipo di classe (struct, class o union), il compilatore sempre dichiarare un costruttore di copia come membro inline public della sua classe. Questo costruttore di copia-implicitamente dichiarato ha la forma T::T(const T&) se tutte le seguenti condizioni:

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.

  • tutte le basi dirette e virtuali di T hanno costruttori di copia con riferimenti a const o volatile, a const come i loro primi parametri

    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.

  • tutti i membri non statici della T hanno costruttori di copia con riferimenti a const o volatile, a const come i loro primi parametri

    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.

In caso contrario, il costruttore di copia-implicitamente dichiarato è T::T(T&). (Si noti che a causa di queste regole, il costruttore di copia-implicitamente dichiarato non può legarsi a un argomento volatili lvalue)

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 classe può avere più costruttori di copia, ad es sia T::T(const T&) e T::T(T&). Se alcuni definiti dall'utente costruttori di copia sono presenti, l'utente può comunque forzare la generazione del costruttore di copia implicitamente dichiarata con la parola chiave default (dal 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 (dal C++11).

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Soppresso implicitamente dichiarato costruttore di copia

Il costruttore di copia implicitamente dichiarate o inadempiente per T classe è (fino al c++11) undefined / definito come cancellati (dal C++11) in una qualsiasi delle seguenti condizioni:

Original:

The implicitly-declared or defaulted copy constructor for class T is undefined (fino al c++11) / defined as deleted (dal 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 ha membri non statici di dati che non possono essere copiati (sono state eliminate, inaccessibili, o costruttori di copia ambigue)

    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 ha classe base diretta o virtuale che non possono essere copiati (ha eliminato, costruttori di copia inaccessibili, 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 ha classe base diretta o virtuale con un distruttore cancellati o inaccessibili

    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 ha definito dall'utente costruttore spostamento o assegnazione (dal C++11) movimento dell'operatore

    Original:

    T has a user-defined move constructor or move assignment operator (dal 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 è un sindacato e ha un membro variante con non banale (dal C++11) costruttore di copia

    Original:

    T is a union and has a variant member with non-trivial copy constructor (dal 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 ha un membro dati di (dal C++11) tipo di riferimento rvalue

    Original:

    T has a data member of rvalue reference type (dal C++11)

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Costruttore di copia Trivial

Il costruttore di copia-implicitamente dichiarato per T classe è banale se tutte le seguenti condizioni:

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 non ha funzioni membro virtuali

    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 non ha classi base virtuali

    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.

  • Il costruttore di copia selezionato per ogni base diretta di T è banale

    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.

  • Il costruttore di copia selezionato per ogni non-static tipo di classe (o una matrice di tipo di classe) memeber di T è banale

    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 costruttore di copia banale è un costruttore che crea una copia a byte della rappresentazione dell'oggetto della discussione, e non esegue alcuna altra azione. Gli oggetti con costruttori di copia banali possono essere copiati copiando le loro rappresentazioni di oggetti manualmente, ad esempio, con std::memmove. Tutti i tipi di dati compatibili con il linguaggio C (tipo POD) sono banalmente copiabili.

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.

Implicitamente definito costruttore di copia

Se il costruttore di copia-implicitamente dichiarato non viene eliminato o banale, è definito (cioè corpo di una funzione viene generato e compilato) da parte del compilatore. Per i tipi union, il costruttore di copia implicitamente definito copia la rappresentazione dell'oggetto (come da std::memmove). Per i non-sindacali tipi di classe (class e struct), il costruttore esegue membro a pieno titolo-saggio copia delle basi dell'oggetto e membri non statici, nel loro ordine di inizializzazione, utilizzando l'inizializzazione diretta.

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 generazione del costruttore di copia è implicitamente definito deprecated(dal C++11) se T ha definito dall'utente o distruttore definito dall'utente operatore di assegnamento per copia.

Original:

The generation of the implicitly-defined copy constructor is deprecated(dal 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.

Note

In molte situazioni, costruttori di copia sono ottimizzati fuori anche se produrrebbero osservabili effetti collaterali, copia elisione vedere

Original:

In many situations, copy constructors are optimized out even if they would produce observable side-effects, see copia elisione

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Esempio

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
}