copy initialization
Aus 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. |
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.
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.
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.
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.
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.
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.
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.
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.
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 desT
oder einer Klasse vonT
abgeleitet, die Konstrukteure vonT
werden untersucht und die beste Übereinstimmung wird durch Überlast Auflösung gewählt. Der Konstruktor wird dann aufgerufen, um das Objekt zu initialisieren .Original:IfT
is a class type and the type of other is cv-unqualified version ofT
or a class derived fromT
, the constructors ofT
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 wennT
Nicht-Klasse-Typ ist, sondern der Typ der other ist ein Klasse-Typ, Umwandlung Sequenzen vom Benutzer definiert, die aus der Art der zu otherT
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:IfT
is a class type, and the type of other is different, or ifT
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 toT
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 VersionT
umzuwandeln .Original:Otherwise (if neitherT
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 ofT
.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.
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.
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.
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 }