static_cast conversion

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.
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
 
Converte tra tipi utilizzando una combinazione di conversioni implicite e definite dall'utente.
Original:
Converts between types using a combination of implicit and user-defined conversions.
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

static_cast <new_type> (expression)
Restituisce un valore di tipo new_type.
Original:
Returns a value of type new_type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica]Spiegazione

Solo le seguenti conversioni può essere fatto con static_cast, tranne quando tali conversioni avrebbe gettato via constness''' o volatilità.
Original:
Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
Se un oggetto temporaneo di new_type tipo può essere dichiarata e inizializzata con expression, come da new_type Temp(expression);, che può comportare le conversioni implicite, una chiamata al costruttore di new_type o una chiamata ad un operatore definito dall'utente di conversione, quindi static_cast<type>(expression) calcola e restituisce il valore di tale oggetto temporaneo.
Original:
If a temporary object of type new_type can be declared and initialized with expression, as by new_type Temp(expression);, which may involve implicit conversions, a call to the constructor of new_type or a call to a user-defined conversion operator, then static_cast<type>(expression) computes and returns the value of that temporary object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Se new_type è un puntatore o un riferimento a qualche D classe e il tipo di expression è un puntatore o un riferimento alla sua non-base virtuale B, static_cast esegue un abbattuto. Tale static_cast non fa controlli di runtime per far sì che tipo di runtime l'oggetto è in realtà D, e può essere utilizzato con sicurezza se questa condizione è garantita con altri mezzi, come ad esempio in sede di attuazione polimorfismo statico. Downcast sicuro può essere fatto con dynamic_cast.
Original:
If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static_cast performs a downcast. Such static_cast makes no runtime checks to ensure that the object's runtime type is actually D, and may only be used safely if this precondition is guaranteed by other means, such as when implementing polimorfismo statico. Safe downcast may be done with dynamic_cast.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Se new_type è un tipo di riferimento rvalue, static_cast converte il valore di expression a xValue. Questo tipo di static_cast viene utilizzato per implementare la semantica si muovono in std::move. (dal C++11)
Original:
If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics in std::move. (dal C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Se new_type è il tipo void (possibilmente cv-qualificata), static_cast scarta il valore di expression dopo aver valutato che.
Original:
If new_type is the type void (possibly cv-qualified), static_cast discards the value of expression after evaluating it.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5) dal new_type al tipo di expression esiste, che non include lvalue a rvalue, array a puntatore, la funzione-di-pointer, puntatore nullo, membro puntatore nullo, o la conversione booleano, quindi in grado di eseguire static_cast l'inverso di tale conversione implicita.
Original:
If an
conversione implicita
Original:
implicit conversion
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
</div>
sequence from new_type to the type of expression exists, that does not include lvalue-to-rvalue, array-to-pointer, function-to-pointer, null pointer, null member pointer, or boolean conversion, then static_cast can perform the inverse of that implicit conversion.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
Se la conversione di expression a new_type comporta lvalue-to-rvalue, array a puntatore, o funzione a puntatore conversione, può essere eseguita in modo esplicito static_cast.
Original:
If conversion of expression to new_type involves lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion, it can be performed explicitly by static_cast.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
Tipo di enumerazione Scoped può essere convertito in un tipo intero o in virgola mobile. (dal C++11)
Original:
Scoped enumeration type can be converted to an integer or floating-point type. (dal C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
8)
Integer, tipo virgola mobile, o enumerazione può essere convertito in qualsiasi tipo di enumerazione (il risultato non è specificato se il valore di expression, convertito tipo sottostante dell'enumerazione, non è uno dei valori di enumerazione di destinazione)
Original:
Integer, floating-point, or enumeration type can be converted to any enumeration type (the result is unspecified if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
9)
Un puntatore a membro di qualche D classe può essere upcast a un puntatore a membro del suo B classe base. Questo static_cast non fa controlli per garantire il membro esiste realmente nel tipo di runtime punta-di opporsi.
Original:
A pointer to member of some class D can be upcast to a pointer to member of its base class B. This static_cast makes no checks to ensure the member actually exists in the runtime type of the pointed-to object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
10)
Un prvalue di tipo puntatore a void (possibilmente cv qualificato) può essere convertito puntatore a qualsiasi tipo. Conversione di un puntatore a puntatore a void e di nuovo al puntatore al originale (o più cv-qualificata) di tipo conserva il suo valore originale.
Original:
A prvalue of type pointer to void (possibly cv-qualified) can be converted to pointer to any type. Conversion of any pointer to pointer to void and back to pointer to the original (or more cv-qualified) type preserves its original value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Come con tutti espressioni cast, il risultato è:
Original:
As with all cast expressions, the result is:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • un lvalue se new_type è un tipo di riferimento o un riferimento lvalue rvalue un funzionamento tipo;
    Original:
    an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • un xValue se new_type è un riferimento rvalue per tipo di oggetto;
    Original:
    an xvalue if new_type is an rvalue reference to object type;
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • un prvalue altrimenti.
    Original:
    a prvalue otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[modifica]Note

static_cast può anche essere usato per disambiguare overload della funzione eseguendo una funzione a puntatore conversione tipo specifico, come in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
Original:
static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica]Parole chiave

static_cast

[modifica]Esempio

#include <vector>#include <iostream>   struct B {};struct D : B {};   enumclass E { ONE, TWO, THREE };enum EU { ONE, TWO, THREE };   int main(){// 1: initializing conversionint n =static_cast<int>(3.14);std::cout<<"n = "<< n <<'\n';std::vector<int> v =static_cast<std::vector<int>>(10);std::cout<<"v.size() = "<< v.size()<<'\n';   // 2: static downcast D d; B& br = d;// upcast via implicit conversion D& another_d =static_cast<D&>(br);// downcast   // 3: lvalue to xvaluestd::vector<int> v2 =static_cast<std::vector<int>&&>(v);std::cout<<"after move, v.size() = "<< v.size()<<'\n';   // 4: discarded-value expressionstatic_cast<void>(v2.size());   // 5. inverse of implicit conversion// todo   // 6. array-to-pointer followed by upcast D a[10]; B* dp =static_cast<B*>(a);   // 7. scoped enum to int or float E e = E::ONE;int one =static_cast<int>(e);   // 8. int to enum, enum to another enum E e2 =static_cast<E>(one); EU eu =static_cast<EU>(e2);   // 9. pointer to member upcast// todo   // 10. void* to any typevoid* voidp =&e;std::vector<int>* p =static_cast<std::vector<int>*>(voidp);}

Output:

n = 3 v.size() = 10 after move, v.size() = 0

[modifica]Vedi anche

close