Namensräume
Varianten
Aktionen

copy 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
 
Initialisiert ein Objekt von einem anderen Objekt
Original:
Initializes an object from another 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 = other ; (1)
f(other); (2)
return other; (3)
catch ( T other) ; (4)
T array [ N ] = { other }; (5)

[Bearbeiten] Erklärung

Copy-Initialisierung wird in den folgenden Situationen ausgeführt:
Original:
Copy 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)
wenn eine benannte Variable (automatische, statische oder threadlokale) mit der Initialisierung aus erklärt wird ein Gleichheitszeichen gefolgt von einem Ausdruck .
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
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 Argument an eine Funktion Wert
Original:
when passing an argument to a function by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
bei der Rückkehr aus einer Funktion, die durch einen Wert zurückgibt
Original:
when returning from a function that returns by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
wenn das Fangen einer Ausnahme nach Wert
Original:
when catching an exception by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
als Teil aggregierten Initialisierung, zu initialisieren jedes Element, für das eine Initialisierung bereitgestellt wird
Original:
as part of aggregierten Initialisierung, to initialize each element for which an initializer is provided
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 Kopie der Initialisierung sind:
Original:
The effects of copy 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 ist eine Klasse-Typ und der Art der other wird cv-unqualifizierte Version des T oder einer Klasse von T abgeleitet, die Konstrukteure von T werden 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 and the type of other is cv-unqualified version of T or a class derived from T, 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.
  • Wenn T ist eine Klasse-Typ, und die Art der other verschieden ist, oder wenn T Nicht-Klasse-Typ ist, sondern der Typ der other ist ein Klasse-Typ, Umwandlung Sequenzen vom Benutzer definiert, die aus der Art der zu other T umwandeln kann untersucht werden und die beste ein durch Überlastung Auflösung gewählt. Das Ergebnis der Umwandlung, die eine temporäre prvalue der Zieltyp ist, wird dann verwendet, um das Objekt leiten zu initialisieren. Der letzte Schritt besteht in der Regel eliminiert und das Ergebnis der Konvertierung wird direkt in dem Speicher für das Zielobjekt zugeordnet konstruiert, aber die Kopie Konstruktors muss zugänglich sein, obwohl dies nicht der Fall verwendet .
    Original:
    If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type, Umwandlung Sequenzen vom Benutzer definiert that can convert from the type of other to T are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to leiten zu initialisieren the object. The last step is usually eliminiert and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls (wenn weder T noch die Art des other sind Klassenarten) werden Standard-Konvertierungen verwendet, falls notwendig, den Wert der an die other cv-uneingeschränkten Version T umzuwandeln .
    Original:
    Otherwise (if neither T nor the type of other are class types), 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

Copy-Initialisierung ist weniger permissive als direkte Initialisierung: copy-Initialisierung nur hält nicht explizit Konstruktoren und benutzerdefinierte Konvertierungsfunktionen .
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Wenn other ein rvalue Ausdruck ist, wird bewegen Konstruktor durch Überlastung Auflösung ausgewählt und aufgerufen werden während des Kopierens-Initialisierung .
Original:
If other is an rvalue expression, bewegen Konstruktor will be selected by overload resolution and called during copy-initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Das Gleichheitszeichen, =, in copy-Initialisierung einer benannten Variable ist nicht mit dem Zuweisungsoperator verwandt. Zuweisungsoperator Überlastungen haben keinen Einfluss auf copy-Initialisierung .
Original:
The equals sign, =, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.
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 <utility>
#include <memory>
 
int main()
{
    std::string s = "test"; // OK: constructor is non-explicit
    std::string s2 = std::move(s); // this copy-initialization performs a move
 
//  std::unique_ptr<int> p = new int(1); // error: constructor is explicit
    std::unique_ptr<int> p(new int(1)); // OK: direct-initialization
 
    int n = 3.14;    // floating-integral conversion
    const int b = n; // const doesn't matter
    int c = b;       // ...either way
}


[Bearbeiten] Siehe auch