Conversión reinterpret_cast
You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar]Sintaxis
reinterpret_cast < new_type> ( expression) | |||||||||
new_type
.new_type
.You can help to correct and verify the translation. Click here for instructions.
[editar]Explicación
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.
expression
. (desde C++11)expression
. (desde C++11)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.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
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.
T1
tipo se puede convertir a puntero a objeto de otro tipo T2
. Si el requisito T2
de alineación no es más estricta que T1
, la conversión del puntero resultante de nuevo a sus rendimientos de tipo de original del valor original, de lo contrario el puntero resultante no puede dejar de hacer referencia de forma segura. En cualquier caso, el puntero resultante sólo puede dejar de hacer referencia de manera segura si es permitido por las reglas de tipo' aliasing (ver más abajo) T1
can be converted to pointer to object of another type T2
. If T2
's alignment requirement is not stricter than T1
's, conversion of the resulting pointer back to its original type yields the original value, otherwise the resulting pointer cannot be dereferenced safely. In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules (see below) You can help to correct and verify the translation. Click here for instructions.
T1
tipo se puede convertir para hacer referencia a otro tipo T2
. El resultado es un valor-o refiriéndose al mismo objeto que el original lvalue, pero con un tipo diferente xValue. Sin temporal se crea ninguna copia se hace, ningún constructor o funciones de conversión son llamados. La referencia de resultado sólo se puede acceder con seguridad si es permitido por las reglas de tipo' aliasing (ver más abajo)T1
can be converted to reference to another type T2
. The result is an lvalue or xvalue referring to the same object as the original lvalue, but with a different type. No temporary is created, no copy is made, no constructors or conversion functions are called. The resulting reference can only be accessed safely if allowed by the type aliasing rules (see below)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.
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.
You can help to correct and verify the translation. Click here for instructions.
T1
clase puede convertirse en un puntero a otro objeto miembro de otra clase T2
. Si la alineación T2
no es más estricta que T1
, la conversión a los rendimientos de tipo de original del valor original, de lo contrario el puntero resultante no puede ser utilizado con seguridad .T1
can be converted to a pointer to another member object of another class T2
. If T2
's alignment is not stricter than T1
's, conversion to the original type yields the original value, otherwise the resulting pointer cannot be used safely.You can help to correct and verify the translation. Click here for instructions.
Como con todas las expresiones de conversión, el resultado es:
- Un l-valor si tipo-de-destino es un tipo referencia a l-valor o un tipo referencia r-valor a función(desde C++11);
| (desde C++11) |
- Un pr-valor en caso contrario.
[editar]Palabras clave
[editar]Alias de tipos
Cuando se intenta leer o modificar el valor almacenado de un objeto de tipo DynamicType
a través de un glvalue de tipo AliasedType
, el comportamiento está indefinido a menos que uno de los siguientes sea verdadero:
AliasedType
yDynamicType
son similares.AliasedType
es la la variante (posiblemente calificada-cv) con signo o sin signo deDynamicType
.AliasedType
es std::byte(desde C++17), char, o unsignedchar: esto permite el examen de la representación de objeto de cualquier objeto como un array de bytes.
Informalmente, dos tipos son similares si, ignorando la calificación-cv de nivel superior:
- son del mismo tipo; o
- ambos son punteros, y los tipos apuntados son similares; o
- ambos son punteros a miembro de la misma clase, y los tipos de los miembros a los que se apunta son similares; o
| (hasta C++20) |
| (desde C++20) |
Por ejemplo:
- constint*volatile* y int**const son similares;
- constint(*volatile S::*const)[20] y int(*const S::*volatile)[20] son similares;
- int(*const*)(int*) y int(*volatile*)(int*) son similares;
- int(S::*)()const y int(S::*)() no son similares;
- int(*)(int*) y int(*)(constint*) no son similares;
- constint(*)(int*) y int(*)(int*) no son similares;
- int(*)(int*const) y int(*)(int*) son similares (son del mismo tipo);
- std::pair<int, int> y std::pair<constint, int> no son similares.
Esta regla habilita el análisis de alias basado en tipo, en el que un compilador supone que el valor leído a través de un glvalue de un tipo no se modifica por una escritura a un glvalue de un tipo diferente (sujeto a las excepciones mencionadas anteriormente).
Observa que varios compiladores de C++ relajan esta regla como una extensión no estándar del lenguaje para permitir el acceso al tipo equivocado a través del miembro inactivo de una unión (tal acceso no está indefinido en C).
[editar]Ejemplo
You can help to correct and verify the translation. Click here for instructions.
#include <cstdint>#include <cassert>#include <iostream>int f(){return42;}int main(){int i =7; // pointer to integer and back uintptr_t v1 =reinterpret_cast<uintptr_t>(&i);// static_cast is an errorstd::cout<<"The value of &i is 0x"<<std::hex<< v1 <<'\n';int* p1 =reinterpret_cast<int*>(v1);assert(p1 ==&i); // pointer to function to another and backvoid(*fp1)()=reinterpret_cast<void(*)()>(f);// fp1(); undefined behaviorint(*fp2)()=reinterpret_cast<int(*)()>(fp1);std::cout<<std::dec<< fp2()<<'\n';// safe // type aliasing through pointerchar* p2 =reinterpret_cast<char*>(&i);if(p2[0]=='\x7')std::cout<<"This system is little-endian\n";elsestd::cout<<"This system is big-endian\n"; // type aliasing through referencereinterpret_cast<unsignedint&>(i)=42;std::cout<< i <<'\n';}
Salida:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
[editar]Ver también
const_cast conversión | agrega o quita const Original: adds or removes const The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
static_cast conversión | realiza conversiones básicas Original: performs basic conversions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
dynamic_cast conversión | realiza conversiones marcadas polimórficos Original: performs checked polymorphic conversions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |