Namensräume
Varianten
Aktionen

value initialization

Aus cppreference.com
< cpp‎ | language

 
 
Sprache C + +
Allgemeine Themen
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.
Flusskontrolle
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.
Bedingte Ausführung Aussagen
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.
Iterationsanweisungen
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.
Gehe Aussagen
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.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
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 Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
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.
Types
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
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
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.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
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.
explizit (C++11)
statisch
Besondere Member-Funktionen
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.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
Bietet die Standard-Ausgangswert zu einem neuen Objekt .
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.

Inhaltsverzeichnis

[Bearbeiten] Syntax

T object {}; (1) (seit C++11)
T();

T{};

(2)
(seit C++11)
new T ();

new T {};

(3)
(seit C++11)

[Bearbeiten] Erklärung

Wert der Initialisierung wird in drei Situationen ausgeführt:
Original:
Value 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)
wenn eine benannte Variable (automatische, statische oder threadlokale) mit der Initialisierung aus einem Paar von Streben erklärt. (seit C++11)
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (seit C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
wenn ein namenloses temporäres Objekt wird mit der Initialisierung, bestehend aus ein leeres Paar Klammern zu erstellt .
Original:
when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
wenn ein Objekt mit dynamischen Lagerdauer wird durch einen neuen Ausdruck mit der Initialisierung, bestehend aus ein leeres Paar Klammern zu erstellt .
Original:
when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die Auswirkungen der Wert der Initialisierung sind:
Original:
The effects of value initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Wenn eine Klasse T Typs mit mindestens einer vom Benutzer bereitgestellten Konstruktor jeglicher Art ist, wird das Standardkonstruktor genannt .
    Original:
    If T is a class type with at least one user-provided constructor of any kind, the Standardkonstruktor is called.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Wenn T ein nicht gewerkschaftlich Klasse Typ ohne Benutzer bereitgestellten Konstruktoren ist, dann ist das Objekt Null initialisiert und dann die implizit deklariert Standardkonstruktor aufgerufen wird (es sei denn, es trivial ist)
    Original:
    If T is an non-union class type without any user-provided constructors, then the object is Null initialisiert and then the implicitly-declared default constructor is called (unless it's trivial)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Wenn T ein Array-Typ ist, jedes Element des Feldes Wert initialisiert
    Original:
    If T is an array type, each element of the array is value-initialized
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls besteht die Aufgabe Null initialisiert .
    Original:
    Otherwise, the object is zero-initialized.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Notes

Die Syntax T object(); ​​nicht initialisiert ein Objekt, es erklärt, eine Funktion, die keine Argumente und gibt T dauert. Der Weg zum Value-Initialisierung eine benannte Variable vor C + 11 war T object = T();, welcher Wert initialisiert eine temporäre und dann copy-initialisiert das Objekt: die meisten Compiler optimiert die Kopie in diesem Fall .
Original:
The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns T. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Referenzen dürfen nicht Wert initialisiert .
Original:
References cannot be value-initialized.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Alle Standard-Containern (std::vector, std::list, etc) Wert initialisieren ihre Elemente, wenn sie mit einem einzigen size_type Argument oder wenn gewachsen durch einen Aufruf resize() gebaut .
Original:
All standard containers (std::vector, std::list, etc) value-initialize their elements when constructed with a single size_type argument or when grown by a call to resize().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Beispiel

#include <string>
#include <vector>
#include <iostream>
 
struct T1 {
    int mem1;
    std::string mem2;
}; // no constructors
struct T2 { 
    int mem1;
    std::string mem2;
    T2(const T2&) {} // a constructor, but no default
};
struct T3 { 
    int mem1;
    std::string mem2;
    T3() {} // user-provided default ctor
};
 
std::string s{}; // calls default ctor, the value is "" (empty string)
int main()
{
    int n{};     // non-class value-initialization, value is 0
    double f = double(); // non-class value-init, value is 0.0
    int* a = new int[10](); // array of 10 zeroes
 
    T1 t1{}; // no ctors: zero-initialized
             // t1.mem1 is zero-initialized
             // t1.mem2 is default-initialized
//    T2 t2{}; // error: has a ctor, but no default ctor
    T3 t3{}; // user-defined default ctor:
             // t3.mem1 is default-initialized (the value is indeterminate)
             // t3.mem2 is default-initialized
 
    std::vector<int> v(3); // value-initializes three ints
 
    std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n';
    std::cout << t1.mem1 << ' ' << t3.mem1 << '\n';
    delete[] a;
}

Output:

0 0 0 0 0
0 4199376

[Bearbeiten] Siehe auch