Namespace
Varianti

reference initialization

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
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.
Controllo del flusso
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.
Dichiarazioni esecuzione condizionale
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.
Iterazione dichiarazioni
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.
Vai dichiarazioni
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.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
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 specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
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.
Tipi
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
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
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.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
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.
esplicito (C++11)
statico
Funzioni membro speciali
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.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
Associa un riferimento a un oggetto
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.

Indice

[modifica] Sintassi

T & ref = object ;

T & ref ( object ) ;

T & ref { object } ;

(1)
T && ref = object ;

T && ref ( object ) ;

T && ref { object } ;

(2) (dal C++11)
R fn ( T & arg );

or

R fn ( T && arg );

fn ( object )

(3)
T & fn () {

or

T && fn () {

return object ;

(4)

[modifica] Spiegazione

Un riferimento T possono essere inizializzati con un oggetto di T tipo, in funzione del tipo di T, o un oggetto convertibile in modo implicito T. Una volta inizializzato, il riferimento non può essere modificato per fare riferimento a un altro oggetto.
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.
I riferimenti vengono inizializzati nelle seguenti situazioni:
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.
1)
Quando una chiamata lvalue variabile di riferimento viene dichiarato con un inizializzatore
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.
2)
Quando un nome variabile di riferimento rvalue viene dichiarata con un inizializzatore
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.
3)
In un'espressione di chiamata di funzione, quando il parametro di funzione ha tipo di riferimento
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.
4)
Nella dichiarazione return, quando la funzione restituisce un tipo di riferimento
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.
Gli effetti di inizializzazione riferimento sono:
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.
  • Se il riferimento è un riferimento lvalue:
    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.
  • Se object è un'espressione lvalue, e il suo tipo è T o una base di T, ed è altrettanto o meno cv qualificati, allora il riferimento è legato all'oggetto identificato dal lvalue o il sotto-oggetto classe base dell'oggetto.
    Original:
    If object is an lvalue expression, and its type is T or a base of T, 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.
  • Se object è un'espressione lvalue, e il suo tipo è convertibile in modo implicito un tipo che è o T o una base di T, ugualmente o meno cv qualificati, i non espliciti funzioni di conversione del tipo di origine e delle sue classi base che restituiscono riferimenti lvalue vengono considerati e la migliore è selezionata per risoluzione di sovraccarico. Il riferimento viene quindi associato all'oggetto identificato dal lvalue restituito dalla funzione di conversione (o il suo sotto-oggetto classe base)
    Original:
    If object is an lvalue expression, and its type is implicitly convertible to a type that is either T or a base of T, 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.
  • In caso contrario, se il riferimento è o riferimento rvalue o riferimenti a lvalue const:
    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.
  • Se object è un xValue, un prvalue classe, un prvalue matrice, o un tipo di funzione che è o lvalue T o una base di T, altrettanto o meno cv qualificato, allora il riferimento è legato al valore dell'espressione di inizializzazione o la sua base di sotto-oggetto.
    Original:
    If object is an xvalue, a class prvalue, an array prvalue, or a function lvalue type that is either T or a base of T, 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.
  • Se object è un'espressione tipo di classe che può essere convertito in modo implicito un xValue, un prvalue classe, o un valore funzione del tipo che sia o T o una base di T, ugualmente o meno cv qualificati, allora il riferimento è legato alla risultato della conversione o del suo sotto-oggetto base.
    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 either T or a base of T, 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.
  • Altrimenti, un temporaneo di tipo T è costruito e copia-inizializzato da object. Il riferimento è quindi associato a questo temporaneo.
    Original:
    Otherwise, a temporary of type T is constructed and copia-inizializzato 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.

[modifica] Durata di una temporanea

Ogniqualvolta un riferimento è associato a un temporaneo o ad un sotto-oggetto base di un temporaneo, la durata della temporanea è esteso a corrispondere alla durata del riferimento, con le seguenti eccezioni:
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.
  • un temporaneo associato a un valore di ritorno di una funzione in un'istruzione return non viene esteso: viene distrutto immediatamente al termine dell'espressione ritorno. Tale funzione restituisce sempre un riferimento penzoloni.
    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.
  • un temporaneo associato a un organo di riferimento in un costruttore inizializzatore lista persiste solo fino alla chiusura del costruttore, non finché l'oggetto esiste.
    Original:
    a temporary bound to a reference member in a constructor inizializzatore lista 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.
  • un temporaneo associato a un parametro di riferimento in una chiamata di funzione presente fino alla fine della piena espressione contenente la chiamata di funzione: se la funzione restituisce un riferimento, che sopravvive la piena espressione, diventa un punto di riferimento penzoloni.
    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.
  • un temporaneo associato a un riferimento nell'inizializzatore utilizzato in una nuova espressione esiste fino alla fine della piena espressione contenente la nuova espressione, non finché l'oggetto inizializzato. Se l'oggetto inizializzato sopravvive la piena espressione, il suo organo di riferimento diventa un riferimento penzoloni.
    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.
In generale, la durata di un temporaneo non può essere ulteriormente prorogato di "passarlo": un secondo riferimento, inizializzata dal riferimento al quale era legata la temporanea, non influisce sulla sua vita.
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.

[modifica] Note

I riferimenti appaiono senza inizializzatori solo in dichiarazione di funzione dei parametri, in dichiarazione di tipo di ritorno della funzione, nella dichiarazione di un membro della classe, e con l'identificatore extern.
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.

[modifica] Esempio

#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;
}


[modifica] Vedi anche