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 ;
}

参考:

https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used/332086#332086

https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast?noredirect=1&lq=1

https://www.quora.com/How-do-you-explain-the-differences-among-static_cast-reinterpret_cast-const_cast-and-dynamic_cast-to-a-new-C++-programmer

http://www.stroustrup.com/bs_faq2.html

http://www.cplusplus.com/doc/tutorial/typecasting/

C++: Type conversions的更多相关文章

  1. 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 ...

  2. Type conversions in C++类型转换

    ###Implicit conversions隐式转换* 可以在基本类型之间自由转换:* 可以把任何类型的pointer转换为void pointer:* 可以将子类pointer转换为基类point ...

  3. [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 ...

  4. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  5. Type Systems

    This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...

  6. Type system

    Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...

  7. Type system-Type checking

    类型系统的属性: 1.结构属性: 2.规则属性:类型系统定义了一套规则(内部数据的访问规则.函数的访问规则.类型的比较与转化规则),以供编译和运行时进行检查. In programming langu ...

  8. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  9. Atitit ABI FFI 的区别与联系 attilax总结

    Atitit ABI FFI 的区别与联系 attilax总结 FFI stands for Foreign Function Interface. A foreign function interf ...

随机推荐

  1. 'ddkbuild.cmd' 不是内部或外部命令,也不是可运行的程序

    转自VC错误:http://www.vcerror.com/?p=49 问题描述: 错误:'ddkbuild.cmd' 不是内部或外部命令,也不是可运行的程序 解决方法: 详细的解决方法可参考VC错误 ...

  2. mysql查看数据库大小或者表大小

    要想知道每个数据库的大小的话,步骤如下: 1.进入information_schema 数据库(存放了数据库的信息) use information_schema; 2.查询所有数据库的大小: sel ...

  3. iOS开发UIResponder之NSUndoManager

    1.简介 UIResponder有个属性:NSUndoManager @property(nullable, nonatomic,readonly) NSUndoManager *undoManage ...

  4. (转)AttributeError: module 'tkinter' has no attribute 'messagebox'

    AttributeError: module 'tkinter' has no attribute 'messagebox' improt tkinter from tkinter import * ...

  5. assignment of day nine

    一.简述定义函数的三种方式 1.空函数:用于占位 2.有参函数:有参数的函数 3.无参函数:没有参数的函数 二.简述函数的返回值 1.如果函数没有返回值,默认返回None 2.函数可以通过return ...

  6. 最接近神的人_NOI导刊2010提高(02)

    题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着“神的殿堂”.小FF猜想里面应该就有王室的 ...

  7. Number Sequence /// oj21456

    题目大意: 有一组规律数 the first 80 digits of the sequence are as follows: 1 12 123 1234 12345 123456 1234567 ...

  8. QT5+Pylon

    VS+QT+Pylon:配置一下包含文件和libs,具体参考pylon说明文档. Windows下  Qtcreator+Pylon:建议把include和libs文件夹拷贝至工作目录,然后修改.pr ...

  9. javascript字符串方法学习汇总

    1.charAt(index) charAt(index):返回字符串中指定位置的字符 var str = 'abcdefghi'; console.log(str.charAt()); // 输出 ...

  10. MySQL 11章_索引、触发器

    一. 索引: . 为什么要使用索引: 一本书需要目录能快速定位到寻找的内容,同理,数据表中的数据很多时候也可以为他们创建相应的“目录”,称为索引,当创建索引后查询数据也会更加高效 . Mysql中的索 ...