Namensräume
Varianten

direct 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.
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
 
Initialisiert ein Objekt aus expliziten Satz Konstruktorargumente .
Original:
Initializes an object from explicit set of constructor arguments.
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

Tobject( arg);

Tobject( arg1, arg2, ...);

(1)
Tobject{ arg};

Tobject{ arg1, arg2, ...};

(2) (seit C++11)
T( other)

T( arg1, arg2, ...);

(3)
static_cast<T>(other) (4)
newT(args, ...) (5)
Class::Class() : member(args, ...) {... (6)
[arg](){... (7) (seit C++11)

[Bearbeiten]Erklärung

Direkter Initialisierung in den folgenden Situationen durchgeführt:
Original:
Direct initialization is performed in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
Initialisierung mit einer nichtleeren geklammerten Liste von Ausdrücken
Original:
initialization with a nonempty parenthesized list of expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
während list-Initialisierung Sequenz, wenn keine Initialisierer-Liste constuctors vorgesehen sind und ein passender Konstruktor zugänglich ist, und alle notwendigen impliziten Konvertierungen sind nicht Verengung .
Original:
during list-Initialisierung sequence, if no initializer-list constuctors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Initialisierung eines prvalue vorübergehend durch funktionale Besetzung oder mit einem geklammerten Ausdruck Liste
Original:
initialization of a prvalue temporary by funktionale Besetzung or with a parenthesized expression list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Initialisierung eines prvalue temporären durch eine static_cast expession
Original:
initialization of a prvalue temporary by a static_cast expession
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
Initialisierung eines Objekts mit dynamischen Lagerdauer von einem neuen Ausdruck mit einem nicht-leeren Initialisierung
Original:
initialization of an object with dynamic storage duration by a new-expression with a non-empty initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
Initialisierung einer Base oder eines nicht-statische Element durch constructor Initialisierungsliste
Original:
initialization of a base or a non-static member by constructor Initialisierungsliste
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
Initialisierung der Schließung Objekt Mitglieder aus den Variablen durch Kopie in einem Lambda-Ausdruck gefangen
Original:
initialization of closure object members from the variables caught by copy in a lambda-expression
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 direkten Initialisierung sind:
Original:
The effects of direct 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 T eine Klasse Typ ist, sind die Konstrukteure von T untersucht und die beste Übereinstimmung wird durch Überlast Auflösung gewählt. Der Konstruktor wird dann aufgerufen, um das Objekt zu initialisieren .
    Original:
    If T is a class type, the constructors of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls, wenn T ein Nicht-Klasse-Typ ist, werden Standard-Konvertierungen verwendet, falls notwendig, um den Wert des an die other cv-uneingeschränkten Version T umzuwandeln .
    Original:
    Otherwise, if T is a non-class type, Standard-Konvertierungen are used, if necessary, to convert the value of other to the cv-unqualified version of T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Notes

Direct-Initialisierung ist toleranter als copy-Initialisierung: copy-Initialisierung nur hält nicht explizit Konstruktoren und benutzerdefinierte Konvertierung Funktionen, während direkte Initialisierung berücksichtigt alle Konstrukteure und implizite Konvertierung Sequenzen .
Original:
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
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 <iostream>#include <memory>   struct Foo {int mem;explicit Foo(int n): mem(n){}};   int main(){std::string s1("test");// constructor from const char*std::string s2(10, 'a');   std::unique_ptr<int> p(new int(1));// OK: explicit constructors allowed// std::unique_ptr<int> p = new int(1); // error: constructor is explicit   Foo f(2);// f is direct-initialized:// constructor parameter n is copy-initialized from the rvalue 2// f.mem is direct-initialized from the parameter n// Foo f2 = 2; // error: constructor is explicit   std::cout<< s1 <<' '<< s2 <<' '<<*p <<' '<< f.mem<<'\n';}

Output:

test aaaaaaaaaa 1 2

[Bearbeiten]Siehe auch

close