◐ Shell
clean mode source ↗

default initialization - cppreference.com

De cppreference.com

<metanoindex/>

Fornece o valor inicial padrão para um novo objeto.

Original:

Provides the default initial value to a new object.

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

Sintaxe

T object ; (1)
new T ; (2)

Explicação

Inicialização padrão é realizada em três situações:

Original:

Default initialization is performed in three situations:

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

1)

quando uma variável com duração de armazenamento automático é declarado sem inicializador

Original:

when a variable with automatic storage duration is declared with no initializer

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

2)

quando um objecto com uma duração de armazenamento dinâmico é criado por uma expressão de novo sem um inicializador

Original:

when an object with dynamic storage duration is created by a new-expression without an initializer

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

3)

quando uma classe base ou um não-estático membro de dados não é mencionado em um lista de inicializador construtor eo construtor é chamado.

Original:

when a base class or a non-static data member is not mentioned in a constructor lista de inicializador and that constructor is called.

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

Os efeitos de inicialização padrão são:

Original:

The effects of default initialization are:

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

  • Se T é um tipo de classe, o construtor padrão é chamado para fornecer o valor inicial para o novo objeto.

    Original:

    If T is a class type, the construtor padrão is called to provide the initial value for the new object.

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

  • Se T é um tipo de matriz, cada elemento da matriz é o padrão-inicializado.

    Original:

    If T is an array type, every element of the array is default-initialized.

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

  • Caso contrário, nada é feito.

    Original:

    Otherwise, nothing is done.

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

Se T é um tipo const qualificado, deve ser um tipo de classe com um construtor padrão fornecido pelo usuário.

Original:

If T is a const-qualified type, it must be a class type with a user-provided default constructor.

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

Notas

Inicialização padrão de não-classe variáveis ​​com tempo de armazenamento automático e dinâmico produz objetos com valores indeterminados (objetos estáticos e linha localidade obter zero inicializado)

Original:

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-locale objects get zero inicializado)

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

Referência não pode ser padrão inicializada.

Original:

Reference cannot be default-initialized.

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

Exemplo

#include <string>
struct T1 {};
class T2 { 
    int mem;
 public:
    T2() {} // "mem" not in initializer list
};
int n; // This is not default-initialization, the value is zero.
int main()
{
    int n;            // non-class: the value is undeterminate
    std::string s;    // calls default ctor, the value is "" (empty string)
    std::string a[2]; // calls default ctor, creates two empty strings
//    int& r;         // error: default-initializing a reference
//    const int n;    // error: const non-class type
//    const T1 nd;    // error: const class type with implicit ctor
    T1 t1; // ok, calls implicit default ctor
    const T2 t2; // ok, calls the user-provided default ctor 
                 // t2.mem is default-initialized
}

Veja também