Naw*_*waz 40 c++ casting static-cast reinterpret-cast
说我想投A*
,char*
反之亦然,我们有两个选择(我的意思是,我们很多人认为我们有两个选择,因为两者似乎都有效!因此混乱!):
struct A
{
int age;
char name[128];
};
A a;
char *buffer = static_cast<char*>(static_cast<void*>(&a)); //choice 1
char *buffer = reinterpret_cast<char*>(&a); //choice 2
Run Code Online (Sandbox Code Playgroud)
两者都很好.
//convert back
A *pA = static_cast<A*>(static_cast<void*>(buffer)); //choice 1
A *pA = reinterpret_cast<A*>(buffer); //choice 2
Run Code Online (Sandbox Code Playgroud)
即便这样工作正常!
那么,为什么我们reinterpret_cast
在C++中有两个链接 static_cast
可以完成它的工作呢?
你们中的一些人可能认为这个主题与之前的主题重复,例如本文底部列出的,但事实并非如此.这些主题讨论只在理论上,但他们没有给出甚至一个例子来展示为什么reintepret_cast
是真正需要的,而2 static_cast
将肯定会失败.我同意,一个static_cast会失败.但两个怎么样?
如果两个链接的语法static_cast
看起来很麻烦,那么我们可以编写一个函数模板,使其对程序员更友好:
template<class To, class From>
To any_cast(From v)
{
return static_cast<To>(static_cast<void*>(v));
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以使用它,如:
char *buffer = any_cast<char*>(&a); //choice 1
char *buffer = reinterpret_cast<char*>(&a); //choice 2
//convert back
A *pA = any_cast<A*>(buffer); //choice 1
A *pA = reinterpret_cast<A*>(buffer); //choice 2
Run Code Online (Sandbox Code Playgroud)
另外,请看这种any_cast
有用的情况:正确转换fstream读写成员函数.
所以我的问题基本上是,
reinterpret_cast
用C++?static_cast
肯定不能做同样的工作?Jam*_*lis 36
有些事情reinterpret_cast
可以做到没有static_cast
s 序列可以做(所有来自C++ 03 5.2.10):
指针可以显式转换为足以容纳它的任何整数类型.
可以将整数类型或枚举类型的值显式转换为指针.
指向函数的指针可以显式转换为指向不同类型函数的指针.
类型"指针的构件的右值X
类型的T1
"可被显式转换为类型的右值"指针的构件Y
类型的T2
"如果T1
和T2
都函数类型或两者的对象类型.
另外,来自C++ 03 9.2/17:
reinterpret_cast
指向其初始成员(或者如果该成员是位字段,则指向它所驻留的单元),反之亦然.sha*_*oth 15
你需要reinterpret_cast
得到一个带有硬编码地址的指针(就像这里):
int* pointer = reinterpret_cast<int*>( 0x1234 );
Run Code Online (Sandbox Code Playgroud)
您可能希望使用此类代码来访问某些内存映射设备输入输出端口.
一个具体的例子:
char a[4] = "Hi\n";
char* p = &a;
f(reinterpret_cast<char (&)[4]>(p)); // call f after restoring full type
// ^-- any_cast<> can't do this...
// e.g. given...
template <typename T, int N> // <=--- can match this function
void f(T (&)[N]) { std::cout << "array size " << N << '\n'; }
Run Code Online (Sandbox Code Playgroud)
除了其他人给出的实际原因之外,他们可以做的事情有所不同,因为它做了不同的工作是件好事.
static_cast说请将类型X的数据转换为Y. reinterpret_cast表示请将X中的数据解释为Y.
很可能底层操作是相同的,并且在许多情况下都可以工作.但是说请将X转换为Y并说"是的我知道这些数据被声明为X但请使用它就好像它真的是Y"之间存在概念上的区别.
归档时间: |
|
查看次数: |
6926 次 |
最近记录: |