Espaços nominais
Variantes
Acções

explicit type conversion

Da cppreference.com
< cpp‎ | language

 
 
Linguagem C + +
Tópicos gerais
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.
Controle de fluxo
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.
Declarações execução condicional
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.
Instruções de iteração
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.
Ir declarações
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.
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaração da função
lambda declaração da função
modelo de função
linha especificador
especificações de exceção (obsoleta)
noexcept especificador (C++11)
Exceções
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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)
Especificadores
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 especificadores
armazenamento duração especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialização
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literais
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressões
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representações alternativas
Utilitários
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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 de alias declaração (C++11)
atributos (C++11)
Conversões
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversões implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Elenco C-estilo e funcional
Alocação de memória
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.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classe propriedades específicas de função
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.
Funções membro especiais
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.
Modelos
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
modelo de classe
modelo de função
especialização de modelo
pacotes de parâmetros (C++11)
Diversos
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Assembly embutido
 
Converte entre os tipos usando uma combinação de conversões explícitas e implícitas.
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.

[editar] Sintaxe

( new_type ) expression (1)
new_type ( expression ) (2)
Retorna um valor de new_type tipo.
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.

[editar] Explicação

1)
Quando a expressão elenco C-estilo é encontrado, o compilador tenta as expressões elenco seguintes, nesta ordem:
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), com extensões: ponteiro ou referência a uma classe derivada é ainda permitido ser convertido para ponteiro ou referência a classe base inequívoca (e vice-versa), mesmo se a classe base é inacessível (ou seja, esse elenco ignora a herança privada especificador). Mesmo se aplica ao elenco ponteiro para ponteiro para membro para membro da base não-virtual unambigous
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 (com extensões) seguido por 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 seguido por 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.
@ @ A primeira escolha que satisfaz as exigências do operador de conversão respectiva for selecionado, mesmo que isso não pode ser compilado (ver exemplo).
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.
@ @ Além disso, estilo C notação elenco é permitido lançar de, para e entre os ponteiros para tipo de classe incompleta. Se ambos expression e new_type são ponteiros para tipos de classe incompletos, é especificado se static_cast ou reinterpret_cast é selecionado.
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)
O molde funcional consiste de um especificador de tipo simples ou um especificador typedef (em outras palavras, uma única palavra de nomes do tipo: unsigned int(expression) ou int*(expression) não são válidos), seguido por uma única expressão entre parêntesis. Este elenco é exatamente equivalente à expressão correspondente elenco C-estilo.
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.
Tal como acontece com todas as expressões fundido, o resultado é:
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.
  • um lvalue se new_type é um tipo de referência ou uma referência lvalue rvalue a funcionar 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.
  • um xValue se new_type é uma referência para rvalue tipo de objeto;
    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.
  • uma outra forma prvalue.
    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.

[editar] Exemplo

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
}