reference 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. |
Wird eine Referenz auf ein Objekt
Original:
Binds a reference to an 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 & ref = object ;
T T |
(1) | ||||||||
T && ref = object ;
T T |
(2) | (seit C++11) | |||||||
R fn ( T & arg );
or R fn fn |
(3) | ||||||||
T & fn () {
or T
|
(4) | ||||||||
[Bearbeiten] Erklärung
Ein Verweis auf
T
kann mit einem Objekt des Typs T
, einer Funktion vom Typ T
oder einem Objekt implizit umwandelbar T
initialisiert werden. Einmal initialisiert, kann ein Verweis nicht geändert, um auf ein anderes Objekt zu beziehen .Original:
A reference to
T
can be initialized with an object of type T
, a function of type T
, or an object implicitly convertible to T
. Once initialized, a reference cannot be changed to refer to 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.
Referenzen werden in den folgenden Situationen initialisiert:
Original:
References are initialized 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 lvalue Führungsgröße wird mit einer Initialisierung deklariert
Original:
When a named lvalue reference variable is declared with an initializer
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 eine benannte rvalue Führungsgröße wird mit einer Initialisierung deklariert
Original:
When a named rvalue reference variable is declared with an initializer
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)
In einem Aufruf der Funktion Ausdruck, wenn die Funktion Parameter Referenz Typ hat
Original:
In a function call expression, when the function parameter has reference type
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)
In der return Aussage, wenn die Funktion liefert eine Referenz-Typ
Original:
In the return statement, when the function returns a reference type
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 Referenz-Initialisierung sind:
Original:
The effects of reference 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 die Referenz ist ein L-Wert-Referenz:Original:If the reference is an lvalue reference:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Wenn object ein L-Wert Ausdruck ist, und seine Art ist
T
oder eine BaseT
und ist gleich oder weniger cv-qualifiziert, dann wird die Referenz auf das Objekt von der L-Wert oder der Basisklasse Unterobjekt des Objekts identifiziert gebunden .Original:If object is an lvalue expression, and its type isT
or a base ofT
, and is equally or less cv-qualified, then the reference is bound to the object identified by the lvalue or the base class subobject of 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 object ein L-Wert Ausdruck ist, und der Typ ist implizit konvertierbar zu einer Art, die entweder
T
oder eine BaseT
ist gleich oder weniger cv-qualifiziert, dann sind die nicht-explizite Konvertierung Funktionen des Quell-Typ und dessen Basis-Klassen, die zurück lvalue Hinweise werden berücksichtigt und das beste ist durch Überlast Auflösung gewählt. Der Verweis wird dann zu dem Objekt durch den L-Wert identifiziert zurückschickt Umwandlungsfunktion (oder seine Basisklasse Subobjekt) gebundenOriginal:If object is an lvalue expression, and its type is implicitly convertible to a type that is eitherT
or a base ofT
, equally or less cv-qualified, then the non-explicit conversion functions of the source type and its base classes that return lvalue references are considered and the best one is selected by overload resolution. The reference is then bound to the object identified by the lvalue returned by the conversion function (or its base class subobject)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Andernfalls, wenn die Bildnummer entweder rvalue Referenz oder Lvalue Bezugnahme auf const ist:Original:Otherwise, if the reference is either rvalue reference or lvalue reference to const:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Wenn ein object xWert, eine Klasse prvalue, ein Array prvalue, oder eine Funktion, die entweder L-Wert Typ
T
oder eine Base derT
ist, gleich oder weniger cv-qualifiziert ist, dann wird der Sollwert auf den Wert der Initialisierer Expression gebunden oder ihre Basis Unterobjekt .Original:If object is an xvalue, a class prvalue, an array prvalue, or a function lvalue type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or its base subobject.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Wenn object ein Klassentyp Ausdruck, der implizit zu einem xWert, einer Klasse prvalue oder einer Funktion Wert des Typs, die entweder
T
oder eine BaseT
ist umgewandelt werden kann, gleich oder weniger cv-qualifiziert ist, dann wird der Verweis auf die gebundene Ergebnis der Umwandlung oder seiner Basis Unterobjekt .Original:If object is a class type expression that can be implicitly converted to an xvalue, a class prvalue, or a function value of type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the result of the conversion or its base subobject.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Andernfalls wird eine temporäre vom Typ
T
konstruiert und copy-initialisiert aus object. Der Verweis wird dann zu dieser temporären gebunden .Original:Otherwise, a temporary of typeT
is constructed and copy-initialisiert from object. The reference is then bound to this temporary.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
[Bearbeiten] Lebensdauer einer temporären
Immer wenn ein Verweis auf eine temporäre oder einer Unterlage Teilobjekt eines temporären gebunden ist, wird die Lebensdauer des temporären verlängert die Lebensdauer des Referenz, mit den folgenden Ausnahmen entsprechen:
Original:
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
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.
- eine temporäre gebunden an einen Rückgabewert einer Funktion in einem return Anweisung nicht verlängert: es wird sofort am Ende der Rückkehrbewegung Expression zerstört. Eine solche Funktion gibt immer einen baumelnden Referenz .Original:a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - eine vorübergehende, gebunden an einen Referenz-Element in einem Konstruktor Initialisierungsliste besteht nur bis zum Konstruktor beendet, nicht so lange wie das Objekt existiert .Original:a temporary bound to a reference member in a constructor Initialisierungsliste persists only until the constructor exits, not as long as the object exists.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - eine vorübergehende, gebunden an einen Referenz-Parameter in einem Aufruf der Funktion besteht bis zum Ende der vollen Ausdruck mit diesem Aufruf der Funktion: Wenn die Funktion liefert eine Referenz, die den vollen Ausdruck überlebt, wird es ein dangling Referenz .Original:a temporary bound to a reference parameter in a function call exists until the end of the full expression containing that function call: if the function returns a reference, which outlives the full expression, it becomes a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - eine temporäre gebunden an eine Referenz in der Initialisierung in einem neuen Ausdruck verwendet besteht bis zum Ende der vollen Ausdruck mit diesem neuen Ausdruck, nicht so lange wie die initialisierte Objekt. Wenn das initialisierte Objekt überlebt den vollen Ausdruck, wird seine Referenz Mitglied ein dangling Referenz .Original:a temporary bound to a reference in the initializer used in a new-expression exists until the end of the full expression containing that new-expression, not as long as the initialized object. If the initialized object outlives the full expression, its reference member becomes a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Im Allgemeinen kann die Lebensdauer einer temporären nicht weiter durch "Weitergabe an" erweitert werden: eine zweite Referenz, von der Bezugnahme auf den die temporäre mußte initialisiert, hat keinen Einfluss auf die Lebensdauer .
Original:
In general, the lifetime of a temporary cannot be further extended by "passing it on": a second reference, initialized from the reference to which the temporary was bound, does not affect its lifetime.
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] Notes
Referenzen erscheinen, ohne Initialisierungen nur in Funktionsparameter Erklärung, in der Funktion Rückgabetyp Erklärung, in der Deklaration einer Klasse Mitglied, und mit dem
extern
specifier .Original:
References appear without initializers only in function parameter declaration, in function return type declaration, in the declaration of a class member, and with the
extern
specifier.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 <utility> #include <sstream> struct S { int mi; const std::pair<int,int>& mp; // reference member }; void foo(int) {} struct A {}; struct B : A { int n; operator int&() { return n; }; }; B bar() {return B(); } //int& bad_r; // error: no initializer extern int& ext_r; // OK int main() { // lvalues int n = 1; int& r1 = n; // lvalue reference to the object n const int& cr(n); // reference can be more cv-qualified volatile int& cv{n}; // any initializer syntax can be used int& r2 = r1; // another lvalue reference to the object n // int& bad = cr; // error: less cv-qualified int& r3 = const_cast<int&>(cr); // const_cast is needed void (&rf)(int) = foo; // lvalue reference to function int ar[3]; int (&ra)[3] = ar; // lvalue reference to array B b; A& base_ref = b; // reference to base subobject int& converted_ref = b; // reference to the result of a conversion // rvalues // int& bad = 1; // error: cannot bind lvalue ref to rvalue const int& cref = 1; // bound to rvalue int&& rref = 1; // bound to rvalue const A& cref2 = bar(); // reference to A subobject of B temporary A&& rref2 = bar(); // same int&& xref = static_cast<int&&>(n); // bind directly to n // int&& copy_ref = n; // error: can't bind to an lvalue double&& copy_ref = n; // bind to an rvalue temporary with value 1.0 // restrictions on temporary lifetimes std::ostream& buf_ref = std::ostringstream() << 'a'; // the ostringstream temporary // was bound to the left operand of operator<<, but its lifetime // ended at the semicolon: buf_ref is now a dangling reference. S a { 1, {2,3} }; // temporary pair {2,3} bound to the reference member // a.mp and its lifetime is extended to match a S* p = new S{ 1, {2,3} }; // temporary pair {2,3} bound to the reference // member a.mp, but its lifetime ended at the semicolon // p->mp is a dangling reference delete p; }