reinterpret_cast const_cast的含义是什么

fangyi1120 2004-08-15 05:30:16
同上
...全文
142 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦想成了相扑 2004-08-26
  • 打赏
  • 举报
回复
参见“C++编程思想”(p398~403,第一版),好书啊,越读越有味!!

基本释义:
static_cast 为了“行为良好”和“行为较好”而使用的映射,包括一些我们可能现在不用的映射(如向上映射和自动类型类型转换);
const_cast 用于映射const和volatile;
dynamic_cast 为了安全类型的向下映射(父类型->子类型);
reinterpret_cast 为了映射到一个完全不同的意思。这个关键词在我们需要把类型映射回原有类型时要用到它。我们映射到的类型仅仅是为了故弄玄虚和其他目的。这是所有映射中最危险的。


而且有一个例子很有趣,大家看一下吧(p401):
#include <string.h>
#include <fstream.h>

ofstream out("reinterpret.out");

class X
{
private:
enum { sz = 5 };
int a[sz];
public:
X() { memset(a, 0, sizeof(int) * sz); } // 初始化
virtual void f() {} // 有了虚函数后,数据成员前就多出了虚表指针
int memSize() { return sizeof(a); }; // 数据成员的总大小
friend ostream& operator << (ostream& os, const X& x) // 重载<<
{
for(int i=0; i<sz; i++)
os << x.a[i] << " ";
return os;
}
};

int main(int argc, char* argv[])
{
X x;
out << x << endl;
int * xp = reinterpret_cast<int*>(&x);
xp[1] = 47;
out << x << endl; // 没有达到想要的效果!!

X x2;
const vptr_size = sizeof(X) - x2.memSize();
long l = reinterpret_cast<long>(&x2);
l += vptr_size;
xp = reinterpret_cast<int*>(l);
xp[1] = 47; // 跨过虚表指针占用的空间
out << x2 << endl; // OK。

out.close(); // 关闭文件流

return 0;
}
wellx 2004-08-15
  • 打赏
  • 举报
回复
到网上下载电子书《effecitve C++》或者《more effecitve C++》看看,候杰翻译的,里面有很详细的解释。

rorot 2004-08-15
  • 打赏
  • 举报
回复
网络资源:

http://publib.boulder.ibm.com/infocenter/comphelp/index.jsp?topic=/com.ibm.vacpp6a.doc/language/ref/expressions_and_operators.keyword_const_cast.htm

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_reinterpret_cast_operator.asp

: )
rorot 2004-08-15
  • 打赏
  • 举报
回复
// test static_cast
// test cosnt_cast
// test reinterpret_cast
#include <iostream>

int main()
{
// b points const int object, so cint_a can not be modified
const int cint_a = 10;
const int* b = &cint_a;
int* c = const_cast< int* > ( b );
std::cout << *c << std::endl;
*c = 20;
std::cout << *c << std::endl;
std::cout << cint_a << std::endl;

// b2 points int object, so cint_a2 can be modified
int cint_a2 = 10;
const int* b2 = &cint_a2;
int* c2 = const_cast< int* > ( b2 );
std::cout << *c2 << std::endl;
*c2 = 20;
std::cout << *c2 << std::endl;
std::cout << cint_a2 << std::endl;

// static_cast 和 reinterpret_cast 操作符修改了操作数类型. 它们不是互逆的;
int int_a = 9;
int int_a2 = 9;
// static_cast 在编译时使用类型信息执行转换,
// 在转换执行必要的检测(诸如指针越界计算, 类型检查). 其操作数相对是安全的.
double db_b = static_cast< double >( int_a );
std::cout << db_b << std::endl;
// 另一方面, reinterpret_cast 仅仅是重新解释了给出的对象的比特模型
// 而没有进行二进制转换. ( 所以一般会伴随着错误发生 )
double db_b2 = reinterpret_cast< double& >( int_a2 );
std::cout << db_b2 << std::endl;
return 0;
}
dhfly 2004-08-15
  • 打赏
  • 举报
回复
还有static_cast
dynamic_cast也是用来代替c的强制类型转换的
fangyi1120 2004-08-15
  • 打赏
  • 举报
回复
可以具体点吗?转换的过程中数据如何变化?
积木 2004-08-15
  • 打赏
  • 举报
回复
const_cast C++提供的曲调变量常量属性的转换运算符

reinterpret_cast 这个转换运算符是进行毫无关联的转化,例如一个指针转化为一个int
什么的。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧