| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4863 人关注过本帖, 6 人收藏
标题:C++中四种类型转换运算符的使用方法
只看楼主 加入收藏
无缘今生
Rank: 2
等 级:新手上路
威 望:3
帖 子:523
专家分:7
注 册:2007-6-25
结帖率:100%
收藏(6)
 问题点数:0 回复次数:6 
C++中四种类型转换运算符的使用方法
C++的四个类型转换运算符已经有很久了,但一直没有弄清楚它们的用法,今天看到一本书上的解释,才大致地的了解了其具体的用法.

具体归纳如下:

[bo]reinterpret_cast[/bo]
该函数将一个类型的指针转换为另一个类型的指针.
这种转换不用修改指针变量值存放格式(不改变指针变量值),只需在编译时重新解释指针的类型就可做到.
reinterpret_cast 可以将指针值转换为一个整型数,但不能用于非指针类型的转换.
例:
//基本类型指针的类型转换
double d=9.2;
double* pd = &d;
int *pi = reinterpret_cast<int*>(pd);  //相当于int *pi = (int*)pd;

//不相关的类的指针的类型转换
class A{};
class B{};
A* pa = new A;
B* pb = reinterpret_cast<B*>(pa);   //相当于B* pb = (B*)pa;

//指针转换为整数
long l = reinterpret_cast<long>(pi);   //相当于long l = (long)pi;


[bo]const_cast[/bo]

该函数用于去除指针变量的常量属性,将它转换为一个对应指针类型的普通变量。反过来,也可以将一个非常量的指针变量转换为一个常指针变量。
这种转换是在编译期间做出的类型更改。
例:
const int* pci = 0;
int* pk = const_cast<int*>(pci);  //相当于int* pk = (int*)pci;

const A* pca = new A;
A* pa = const_cast<A*>(pca);     //相当于A* pa = (A*)pca;

出于安全性考虑,const_cast无法将非指针的常量转换为普通变量。


[bo]static_cast[/bo]

该函数主要用于基本类型之间和具有继承关系的类型之间的转换。
这种转换一般会更改变量的内部表示方式,因此,static_cast应用于指针类型转换没有太大意义。
例:
//基本类型转换
int i=0;
double d = static_cast<double>(i);  //相当于 double d = (double)i;

//转换继承类的对象为基类对象
class Base{};
class Derived : public Base{};
Derived d;
Base b = static_cast<Base>(d);     //相当于 Base b = (Base)d;


[bo]dynamic_cast[/bo]

它与static_cast相对,是动态转换。
这种转换是在运行时进行转换分析的,并非在编译时进行,明显区别于上面三个类型转换操作。
该函数只能在继承类对象的指针之间或引用之间进行类型转换。进行转换时,会根据当前运行时类型信息,判断类型对象之间的转换是否合法。dynamic_cast的指针转换失败,可通过是否为null检测,引用转换失败则抛出一个bad_cast异常。
例:
class Base{};
class Derived : public Base{};

//派生类指针转换为基类指针
Derived *pd = new Derived;
Base *pb = dynamic_cast<Base*>(pd);

if (!pb)
    cout << "类型转换失败" << endl;

//没有继承关系,但被转换类有虚函数
class A(virtual ~A();)   //有虚函数
class B{}:
A* pa = new A;
B* pb  = dynamic_cast<B*>(pa);

如果对无继承关系或者没有虚函数的对象指针进行转换、基本类型指针转换以及基类指针转换为派生类指针,都不能通过编译。
搜索更多相关主题的帖子: 运算符 类型 
2008-09-27 17:02
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
回复 1# 无缘今生 的帖子
(1) const_cast:该函数用于去除指针变量的常量属性,既然这样,为什么当初不把这个变量设置成常量或者非常量
(2)const_cast是一个关键字吧?不能说它是一个函数
2008-09-28 10:07
无缘今生
Rank: 2
等 级:新手上路
威 望:3
帖 子:523
专家分:7
注 册:2007-6-25
收藏
得分:0 
回复 2# vfdff 的帖子
const_cast Operator
Grammar

postfix-expression:
const_cast < type-id > ( expression )
The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unaligned qualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.

You cannot use the const_cast operator to directly override a constant variable's constant status.

The const_cast operator converts a null pointer value to the null pointer value of the destination type.

以上是从MSDN网站上复制下来的.
根据其使用方式,个人认为,完全有理由把它们看成是(模板)函数.

时不再来!!!
2008-09-29 18:57
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
收藏
得分:0 
顶一下好像那个dynamic_cast只能用于使用了多态的类。。

不嫌恶地狱,不迷恋人间,不羡慕天堂。
2008-09-29 19:41
mbstorm
Rank: 1
等 级:新手上路
帖 子:166
专家分:0
注 册:2008-10-31
收藏
得分:0 
顶一个
2008-11-01 22:56
Linzxnju
Rank: 1
来 自:盐城
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-10-27
收藏
得分:0 
顶。。。

在通往牛X的路上我一路狂奔。。。
2008-11-29 19:48
快速回复:C++中四种类型转换运算符的使用方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018770 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved