Default constructors
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. |
Un costruttore predefinito è un costruttore che può essere chiamato senza argomenti (sia definito con un elenco di parametri vuoto, o con argomenti di default previsti per ogni parametro). Un tipo con un costruttore pubblico predefinito è
DefaultConstructible
.Original:
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is
DefaultConstructible
.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.
Indice |
[modifica] Sintassi
ClassName ( ) ;
|
(1) | ||||||||
ClassName :: ClassName ( ) body
|
(2) | ||||||||
ClassName() = delete ;
|
(3) | (dal C++11) | |||||||
ClassName() = default ;
|
(4) | (dal C++11) | |||||||
[modifica] Spiegazione
1)
Dichiarazione di un costruttore predefinito
Original:
Declaration of a default constructor
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)
Definizione della funzione di costruzione al di fuori del corpo della classe
Original:
Definition of the constructor outside the class body
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)
Inibire la generazione automatica di un costruttore predefinito
Original:
Inhibiting the automatic generation of a default constructor
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)
Esplicitamente costringendo la generazione automatica di un costruttore predefinito
Original:
Explicitly forcing the automatic generation of a default constructor
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.
ClassName
è l'identificatore della classe contenitriceOriginal:
ClassName
is the identifier of the enclosing classThe 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.
I costruttori di default vengono chiamati quando:
Original:
The default constructors are called when:
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.
- creazione di oggetti o matrici di statica, la durata di archiviazione locale del thread, e automatico che vengono dichiarati senza un inizializzatore, (T obj; o T arr[10];)Original:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T arr[10];)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la creazione di oggetti di durata di conservazione dinamica quando la nuova espressione viene scritto senza un inizializzatore (new T;)Original:creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la creazione di array di durata di stoccaggio dinamico con il new T[n] espressioneOriginal:creating arrays of dynamic storage duration with the expression new T[n]The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - creazione di valore-inizializzati gli oggetti temporanei con l'espressione cast T().Original:creating value-initialized temporary objects with the cast expression T().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 predefinito
Se non definiti dall'utente costruttori di qualsiasi tipo sono previsti per un tipo di classe (struct, class o union), il compilatore sempre dichiarare un costruttore predefinito come membro
inline public
della sua classe. Se alcuni definiti dall'utente costruttori sono presenti, l'utente può comunque forzare la generazione del costruttore implicitamente dichiarata con la parola chiave default
(dal C++11).Original:
If no user-defined constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an
inline public
member of its class. If some user-defined constructors are present, the user may still force the generation of the implicitly declared 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.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Soppresso costruttore predefinito implicito dichiarato
Il costruttore di default implicitamente dichiarate o inadempiente per
T
classe è (fino al c++11) undefined / definito come cancellati (dal C++11) se una delle seguenti condizioni:Original:
The implicitly-declared or defaulted default constructor for class
T
is undefined (fino al c++11) / defined as deleted (dal C++11) if 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.
You can help to correct and verify the translation. Click here for instructions.
-
T
ha un membro del tipo di riferimento (senza una coppia-o-uguale initializer(dal C++11)).Original:T
has a member of reference type (without a brace-or-equal initializer(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 elemento const (senza initializer(dal C++11) tutore-o-uguale) o una definita dall'utente costruttore di default.Original:T
has a const member (without a brace-or-equal initializer(dal C++11)) or a user-defined default constructor.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 (senza initializer(dal C++11) tutore-o-uguale), che ha un costruttore predefinito cancellato, o il suo costruttore di default è ambigua o inaccessibili da questo costruttore.Original:T
has a member (without a brace-or-equal initializer(dal C++11)), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
ha una base diretta o virtuale che ha un costruttore predefinito cancellato, o è ambiguo o inaccessibili da questo costruttore.Original:T
has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
ha una base diretta o virtuale che ha un distruttore cancellato, o un distruttore che è inaccessibile da questo costruttore.Original:T
has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
è un union con almeno un membro di variante con non banale constructor(dal C++11) predefinito.Original:T
is a union with at least one variant member with non-trivial default 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
è un union e tutti i suoi membri sono varianti const.Original:T
is a union and all of its variant members are const.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 default Trivial
Il costruttore predefinito implicito dichiarato per
T
classe è banale se tutte le seguenti condizioni:Original:
The implicitly-declared default 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.
You can help to correct and verify the translation. Click here for instructions.
-
T
non ha funzioni membro virtualiOriginal:T
has no virtual member functionsThe 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 virtualiOriginal:T
has no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
non ha membri non statici con inizializzatori di coppia-o-uguale (dal C++11)Original:T
has no non-static members with brace-or-equal initializers (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. - Ogni base diretta di
T
dispone di un costruttore predefinito sempliceOriginal:Every direct base ofT
has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Ogni membro non statico di tipo classe dispone di un costruttore predefinito sempliceOriginal:Every non-static member of class type has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Un costruttore predefinito banale è un costruttore che non esegue alcuna azione. Gli oggetti con costruttori di default banali possono essere creati utilizzando reinterpret_cast su qualsiasi deposito, ad esempio, sulla memoria allocata con std::malloc. Tutti i tipi di dati compatibili con il linguaggio C (tipo POD) sono banalmente default-costruibile.
Original:
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_cast on any storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.
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.
[modifica] Implicitamente definito costruttore predefinito
Se il costruttore predefinito implicito dichiarati non viene eliminato o banale, è definito (cioè, un corpo di funzione viene generato e compilato) dal compilatore, ed ha esattamente lo stesso effetto come definito dall'utente costruttore con corpo vuoto e vuoto inizializzatore lista. Cioè, chiama i costruttori predefiniti delle basi e dei membri non statici di questa classe.
Original:
If the implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class.
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.
[modifica] Esempio
struct A { int x; A(int x = 1) : x(x) {} // user-defined default ctor }; struct B : A { // B::B() is implicitly-defined, calls A::A() }; struct C { A obj; // C::C() is implicitly-defined, calls A::A() }; struct D : A { D(int y) : A(y) {} // D::D() is not declared because another constructor exists }; struct E : A { E(int y) : A(y) {} E() = default; // explicitly defaulted, calls A::A() }; struct F { int& ref; // reference member const int c; // const member // Bad::Bad() is deleted }; int main() { A a; B b; // D d; // compile error E e; // F f; // compile error }