C++: Type conversions
1、static_cast
static_cast可以转换相关联的类,可以从子类转换成父类。也能从父类转向子类,但是如果转换的父类指针(或者父类引用)所指向的对象是完整的,那么是没有问题;但是如果所指向的对象并不完整,那么会出现runtime错误。
static_cast相对于dynamic_cast而言,除了能转换指针和引用,还能应用于任何能够隐式转换的情况。
2、const_cast
const_cast如它的名字,它是去除修饰在对象上的const和volatile。
3、dynamic_cast
我们从适用范围来了解这个操作。
(1)首先dynamic_cast能够应用于指针转换。
子类指针转换成父类指针,成功;
父类指针转换成子类指针,就分为两种情况:
<a>父类指针p如果真的指向子类对象,那么转换时成功的;
<b>反之,失败,dynamic_cast返回NULL。
(2)其次dynamic_cast能够应用与引用之间的转换(与指针类似)。
子类引用转换成父类引用,成功;
父类引用转换成子类引用,就分为两种情况:
<a>父类引用ob,如果真的指向子类对象,那么转换时成功的;
<b>反之,失败,dynamic_cast,会抛出bad_cast异常。
(3)其他将null指针,转换成任何类型的指针;将任何类型的指针转换成void*类型的指针。
4、reinterpret_cast
reinterpret_cast和上面讲到的两个cast,适用范围更加广泛。它可以适用于任何类型指针之间的转换。
该操作不会去进行动态类型或者静态类型的检测,它仅仅将值强行赋值过去。从某种意义上对编译器进行了一种欺骗,同时也带来了一定的不安全性。所以在使用这个cast的时候,要慎重。下面是这个操作的适用情况:
(1)int和指针之间的相互转换;
(2)无关联类指针之间的转换;
(3)函数指针之间的转换。
#include <iostream> void test_static_cast()
{
float f = 1.012;
int i = static_cast<int>(f); std::cout << f << " " << i << std::endl;
} class CBase
{
protected:
int m_data; public:
virtual void fun() { }
}; class CSub1 : public CBase
{
protected:
int m_data_a;
}; class CSub2 : public CBase
{
protected:
long m_data_b;
}; void test_dynamic_cast()
{
CBase * pb = new CBase();
CSub1 * p1 = new CSub1(); char strTrue[] = "true";
char strFalse[] = "false"; CBase * p = dynamic_cast<CBase*>(p1);
std::cout << "dynamic_cast<CBase *>(p1); is ok? "<< ((p != NULL) ? "true" : "false") << std::endl; CSub1 * pSub1 = dynamic_cast<CSub1*>(pb);
std::cout << "dynamic_cast<CSub1 *>(pb); is ok? "<< ((pSub1 != NULL)? "true" : "false") << std::endl; delete pb;
delete p1; try
{
CBase obb;
CSub1 obsub1;
CBase& ob1 = dynamic_cast<CBase &>(obsub1);
CSub1& ob2 = dynamic_cast<CSub1 &>(obb);
}
catch(std::bad_cast e)
{ }
} class A
{
public:
A(int i)
: m_data(i)
{ } int m_data;
}; class B
{
public:
B(float f)
: m_data(f)
{ } float m_data;
}; class C
{
public:
C(long long ll)
: m_date(ll)
{ } long long m_date;
}; void test_reinterpret_cast()
{
A* pa = new A();
B* pb = new B(1.12);
C* pc = new C(); A* p1 = reinterpret_cast<A*>(pb);
std::cout << "reinterpret_cast<A*>(pb); is ok? "<< ((p1 != NULL) ? "true": "false") << std::endl;
std::cout << p1->m_data << std::endl; p1 = reinterpret_cast<A*>(pc);
std::cout << "reinterpret_cast<A *>(pc); is ok? "<< ((p1 != NULL) ? "true": "false") << std::endl;
std::cout << p1->m_data << std::endl; delete pa;
delete pb;
delete pc;
} int main()
{
test_static_cast();
test_dynamic_cast();
test_reinterpret_cast(); return ;
}
参考:
http://www.stroustrup.com/bs_faq2.html
http://www.cplusplus.com/doc/tutorial/typecasting/
C++: Type conversions的更多相关文章
- A Tour of Go Type conversions
The expression T(v) converts the value v to the type T. Some numeric conversions: var i int = 42 var ...
- Type conversions in C++类型转换
###Implicit conversions隐式转换* 可以在基本类型之间自由转换:* 可以把任何类型的pointer转换为void pointer:* 可以将子类pointer转换为基类point ...
- [Compose] 20. Principled type conversions with Natural Transformations
We learn what a natural transformation is and see the laws it must obey. We will see how a natural t ...
- 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)
NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- Type system
Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...
- Type system-Type checking
类型系统的属性: 1.结构属性: 2.规则属性:类型系统定义了一套规则(内部数据的访问规则.函数的访问规则.类型的比较与转化规则),以供编译和运行时进行检查. In programming langu ...
- 菜鸟学Struts2——零配置(Convention )
又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...
- Atitit ABI FFI 的区别与联系 attilax总结
Atitit ABI FFI 的区别与联系 attilax总结 FFI stands for Foreign Function Interface. A foreign function interf ...
随机推荐
- NX二次开发-创建经典工具栏UF_UI_create_toolbar
NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...
- NXOpenC#_Training_intro(cn)【转载】
- jsp-提交表单乱码解决
jsp提交表单有两种方式,一种是get,一种是post,对于两种方式都可能出现乱码,以下给出两种乱码方式的解决方案. 1.post提交解决乱码 //设置解码方式,post提交解决乱码 比较简单 req ...
- HDU1556-Color the ball-前缀和/线段树/树状数组
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...
- 《转》python 11 表达式和语句
转自 http://www.cnblogs.com/BeginMan/p/3164600.html 一.Python语句 if语句.else语句.elif语句.条件表达式.while语句.for语句. ...
- Nginx安装及分流多个web服务
Ngnix安装及常用配置 一.安装Nginx 1.检查依赖 yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib ...
- ubuntu卸载node和npm
sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove --purge nodej ...
- PHP算法之按奇偶排序数组
给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4]输出:[2,4,3,1]输出 ...
- Altera: set pin locations using tcl
1, compile the project; 2, store current tcl settings: Project –> Generate Tcl File from Project- ...
- 10月23日——作业1——while循环练习
while循环'''此类编程题,注意带进去试一试1.九九乘法表row=1while row<=9: col=1 while col<=row: print(col,"*" ...