Namespace
Varianti

Copy constructors

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
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.
Controllo del flusso
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.
Dichiarazioni esecuzione condizionale
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.
Iterazione dichiarazioni
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.
Vai dichiarazioni
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.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
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)
Specifiers
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 specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
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
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della 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.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
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.
esplicito (C++11)
statico
Funzioni membro speciali
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.
costruttore di default
costruttore di copia
spostare costruttore (C++11)
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
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.

Indice

[modifica] Sintassi

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

[modifica] 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.

[modifica] 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.

[modifica] 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.

[modifica] 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.

[modifica] 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.

[modifica] 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.

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