dynamic_cast, reinterpret_cast, static_cast and
const_cast
在C语言中,经常会遇到类型转换的一些东西。类型转换分为:显示类型转换和隐式类型转换。
隐式类型转换:
所谓隐式类型转换就是语言本身,使用编译器,自动就给我们转成了我们需要的类型
比如如下代码:
short a=2000;
int b;
b=a;
但是要主要一点,数据类型转换,可以低长度的转换成高长度的,否则会丢失数据,出现意想不到的问题。
显示类型转换:
在c语言里,我们使用
short a=2000;
int b;
b = (int) a; // c-like cast notation
b = int (a); // functional notation
但是在c++中,我们使用显示类型转换使用了如下四个关键字
dynamic_cast, reinterpret_cast, static_cast and
const_cast
使用方法,如
dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)
尖括号里是我们想要expression转换而成的类型。
他们都各司其职,在不同的语义环境下使用,
1.dynamic_cast
只能使用在类的指针或者引用之上。因此我们使用它子类转换成父类时候使用
如下例子:
class CBase { };
class CDerived: public CBase { };
CBase b; CBase* pb;
CDerived d; CDerived* pd;
pb = dynamic_cast<CBase*>(&d); // 可以: derived-to-base
pd = dynamic_cast<CDerived*>(&b); // 错误: base-to-derived
完成实现
// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;
class CBase { virtual void dummy() {} };
class CDerived: public CBase { int a; };
int main () {
try {
CBase * pba = new CDerived;
CBase * pbb = new CBase;
CDerived * pd;
pd = dynamic_cast<CDerived*>(pba);
if (pd==0) cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast<CDerived*>(pbb);
if (pd==0) cout << "Null pointer on second type-cast" << endl;
} catch (exception& e) {cout << "Exception: " << e.what();}
return 0;
}
从以上例子可以看到,成功转换是返回其地址,如果没成功,就返回一个0值。
2.static_cast
常量性的转换,比如我们可以将int转换成float类型,将父类转换成子类的类型。但是这个并不在运行时检查类型安全,类型安全取决于程序员。
class CBase {};
class CDerived: public CBase {};
CBase * a = new CBase;
CDerived * b = static_cast<CDerived*>(a);
double d=3.14159265;
int i = static_cast<int>(d);
3.const_cast
This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order to pass a const argument to a function that expects a non-constant parameter:
解除常量性变量。
// const_cast
#include <iostream>
using namespace std;
void print (char * str)
{
cout << str << endl;
}
int main () {
const char * c = "sample text";
print ( const_cast<char *> (c) );
return 0;
}
可以可以正常输出。并不会报编译异常
4.reinterpret_cast
像低级类型转换。
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed:
neither the content pointed nor the pointer type itself is checked.
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a);
最后,想说的是,不建议使用强制类型转换,Effective c++ 条款27:尽量少做转型动作。
更多文章,欢迎访问:http://blog.csdn.net/wallwind,转载请注明出处
分享到:
相关推荐
例说 const_cast,reinterpret_cast,static_cast,dynamic_cast代码.rar
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
主要介绍了C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结,需要的朋友可以参考下
1.static_cast对类的指针只能转换有继承关系的类。对普通的指针来说只能在void*和其他指针之间转换。它还可转换简单的类型,比如int到char等。不能提供数字到指针的转换。不能提供不同类型指针之间的转换比如int*到...
C-style cast举例: int i; double d; i = (int) d;上面的代码就是本来为double类型的d,通过(int)d将其转换成整形值,并将该值赋给整形变量i (注意d本身的值并没有发生改变)。这就是典型的c-style类型转换。下面...
使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。 const_cast,字面上理解就是去const属性。 static_cast,命名上理解是静态类型转换。如int转换成char。 dynamic_cast,...
呵呵,今天来好好看看着几个转换操作符的用法。以前老是看着眼熟,但是用着手生。今天决定搞定这些个东西。在C语言中类型转换有几种方式:1.(expression).在表达式外边加括号,由编译器来决定怎么改变。...
什么是static_cast,dynamic_cast以及reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式。为了类型转换一个简单对象为另一个对象你会使用传统...
本文详细介绍了C++中的四个用与强制类型转换的运算符:用来修改类型的const 或volatile 属性的const_cast,用来修改操作数类型的reinterpret_cast,static_cast,dynamic_cast
C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast
C++四中常用强制类型转化, :static_cast、dynamic_cast、reinterpret_cast、和const_cast
xxx_cast是一个统称,它指的是static_cast(静态转换),const_cast(常量转换),reinterpert_cast(重解释转换),dynamic_cast(动态转换)。本次我们就了解一下他的用途及其转换方式。 1.static_cast(静态转换) ...
.背景知识/名词解释: C++: ? static ? const ? volatile ? mutable ? namespace ? template ? explicit ? const_cast ? static_cast ? dynamic_cast ? reinterpret_cast
const_cast; static_cast; reinterpret_cast; 一、dynamic_cast 该运算符我在之前的文章中已经介绍过了 //www.jb51.net/article/123252.htm 总之,该运算符的语法如下: dynamic_cast < type> (expression) ...
C++ 中的强制类型转换,显示转换也成为强制类型转换(cast),包括以下列名字命名的强制类型转换操作符:static_cast、dynamic_cast、const_cast、reinterpret_cast。
C++更加安全,增加了const常量、引用、四类cast转换(static_cast、dynamic_cast、const_cast、reinterpret_cast)、智能指针、try—catch等等; C++可复用性高,C++引入了模板的概念,后面在此基础上,实现了方便...