Namespace
Varianti

explicit type 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.
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
 
Converte tra i tipi utilizzando una combinazione di conversioni esplicite ed implicite.
Original:
Converts between types using a combination of explicit and implicit conversions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Sintassi

( new_type ) expression (1)
new_type ( expression ) (2)
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

1)
Quando il C-stile di espressione cast viene rilevato, il compilatore tenta le seguenti espressioni cast, in questo ordine:
Original:
When the C-style cast expression is encountered, the compiler attempts the following cast expressions, in this order:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a) const_cast<new_type>(expression)
b)
static_cast<new_type>(expression), con le estensioni: puntatore o un riferimento a una classe derivata è inoltre consentito di eseguire il cast puntatore o un riferimento alla classe base inequivocabile (e viceversa), anche se la classe base non è accessibile (vale a dire, questo cast ignora il privato eredità specificatore). Lo stesso vale per la fusione puntatore a membro di puntatore a membro di inequivocabile non-base virtuale
Original:
static_cast<new_type>(expression), with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambigous non-virtual base
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
c)
static_cast (con estensioni) seguita da const_cast
Original:
static_cast (with extensions) followed by const_cast
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
d) reinterpret_cast<new_type>(expression)
e)
reinterpret_cast seguita da const_cast
Original:
reinterpret_cast followed by const_cast
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
@ @ La prima scelta che soddisfa i requisiti del rispettivo operatore cast è selezionata, anche se non può essere compilato (vedi esempio).
Original:
@@ The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled (see example).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
@ @ Inoltre, C-notazione cast è permesso di gettare da, verso e tra puntatori a tipo di classe incompleto. Se entrambi expression e new_type sono puntatori a tipi di classe incompleti, è specificato se static_cast o reinterpret_cast viene selezionato.
Original:
@@ In addition, C-style cast notation is allowed to cast from, to, and between pointers to incomplete class type. If both expression and new_type are pointers to incomplete class types, it's unspecified whether static_cast or reinterpret_cast gets selected.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Il cast funzionale costituito da un identificatore di tipo semplice oppure un identificatore typedef (in altre parole, una parola singola name Tipo: unsigned int(expression) o int*(expression) non sono validi), seguita da una singola espressione in parentesi. Questo cast è esattamente equivalente al corrispondente stile C espressione cast.
Original:
The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.
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] Esempio

double f = 3.14;
unsigned int n1 = (unsigned int)f; // C-style cast
unsigned int n2 = unsigned(f);     // functional cast
 
class C1;
class C2;
C2* foo(C1* p)
{
    return (C2*)p; // casts incomplete type to incomplete type
}
 
// In this example, C-style cast is interpreted as static_cast
// even though it would work as reinterpret_cast
struct A {};
struct I1 : A {};
struct I2 : A {};
struct D : I1, I2 {};
 
int main()
{
   D* d = nullptr;
   A* a = (A*)d;                   // compile-time error
   A* a = reinterpret_cast<A*>(d); // this compiles
}