找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 292|回复: 5

C++中四种类型转换运算符的使用方法

[复制链接]
发表于 2009-11-5 01:17:32 | 显示全部楼层 |阅读模式 IP:江苏扬州
C++的四个类型转换运算符已经有很久了,但一直没有弄清楚它们的用法,今天看到一本书上的解释,才大致地的了解了其具体的用法.

具体归纳如下:

reinterpret_cast
该函数将一个类型的指针转换为另一个类型的指针.
这种转换不用修改指针变量值存放格式(不改变指针变量值),只需在编译时重新解释指针的类型就可做到.
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;


const_cast

该函数用于去除指针变量的常量属性,将它转换为一个对应指针类型的普通变量。反过来,也可以将一个非常量的指针变量转换为一个常指针变量。
这种转换是在编译期间做出的类型更改。
例:
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无法将非指针的常量转换为普通变量。


static_cast

该函数主要用于基本类型之间和具有继承关系的类型之间的转换。
这种转换一般会更改变量的内部表示方式,因此,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;


dynamic_cast

它与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);

如果对无继承关系或者没有虚函数的对象指针进行转换、基本类型指针转换以及基类指针转换为派生类指针,都不能通过编译。
发表于 2009-11-5 01:17:33 | 显示全部楼层 IP:江苏扬州
(1) const_cast:该函数用于去除指针变量的常量属性,既然这样,为什么当初不把这个变量设置成常量或者非常量
(2)const_cast是一个关键字吧?不能说它是一个函数
回复

使用道具 举报

发表于 2009-11-5 01:17:35 | 显示全部楼层 IP:江苏扬州
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网站上复制下来的.
根据其使用方式,个人认为,完全有理由把它们看成是(模板)函数.
回复

使用道具 举报

发表于 2009-11-5 01:17:36 | 显示全部楼层 IP:江苏扬州
顶一下好像那个dynamic_cast只能用于使用了多态的类。。
回复

使用道具 举报

发表于 2009-11-5 01:17:38 | 显示全部楼层 IP:江苏扬州
顶一个
回复

使用道具 举报

发表于 2009-11-5 01:17:39 | 显示全部楼层 IP:江苏扬州
顶。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 12:25 , Processed in 0.114690 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表