◐ Shell
clean mode source ↗

direct initialization – cppreference.com

Aus cppreference.com

<metanoindex/>

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.

Syntax

T object ( arg );

T object ( arg1, arg2, ... );

(1)
T object { arg };

T object { arg1, arg2, ... };

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

T ( arg1, arg2, ... );

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

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:

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.

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.

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:

Siehe auch