default initialization
Материал из cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Обеспечивает по умолчанию начальное значение нового объекта.
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.
You can help to correct and verify the translation. Click here for instructions.
Содержание |
[править] Синтаксис
T object ;
|
(1) | ||||||||
new T ;
|
(2) | ||||||||
[править] Объяснение
Инициализации по умолчанию выполняется в трех ситуациях:
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.
You can help to correct and verify the translation. Click here for instructions.
1)
При переменной с автоматическим срок хранения объявлена, не инициализатор
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.
You can help to correct and verify the translation. Click here for instructions.
2)
когда объект с динамическим срок хранения создано новое выражение без инициализации
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.
You can help to correct and verify the translation. Click here for instructions.
3)
, когда базовый класс или не статические данные члены не упоминается в конструкторе инициализатор список и что конструктор вызывается.
Original:
when a base class or a non-static data member is not mentioned in a constructor инициализатор список 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.
You can help to correct and verify the translation. Click here for instructions.
Последствия инициализации по умолчанию являются:
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.
You can help to correct and verify the translation. Click here for instructions.
- Если
Tэто класс, конструктор по умолчанию призвана обеспечить начальное значение для нового объекта.Original:IfTis a class type, the конструктор по умолчанию 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.
- Если
Tявляется типом массива, каждый элемент массива по умолчанию инициализирован.Original:IfTis 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.
- В противном случае, ничего не делается.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.
Если
T является константной квалифицированных типа, она должна быть типа класса с предоставленным пользователем конструктор по умолчанию.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.
You can help to correct and verify the translation. Click here for instructions.
[править] Notes
Инициализации по умолчанию не-переменных класса с автоматической и динамической длительность хранения производит объектов с неопределенными значениями (статические и потока объектов язык получите нулю инициализирован)
Original:
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-locale objects get нулю инициализирован)
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.
Ссылка не может быть по умолчанию инициализирован.
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.
You can help to correct and verify the translation. Click here for instructions.
[править] Пример
#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 }