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. (转)虚拟IP原理

    转:http://blog.csdn.net/whycold/article/details/11898249 高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划 ...

  2. HDU-6070 Dirt Ratio(二分+线段树+分数规划)

    目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门  原题目描述在最下面.  求\(sum/len\)最小值.\(sum\)是一段区间内不同数字的 ...

  3. 秒懂机器学习---k临近算法(KNN)

    秒懂机器学习---k临近算法(KNN) 一.总结 一句话总结: 弄懂原理,然后要运行实例,然后多解决问题,然后想出优化,分析优缺点,才算真的懂 1.KNN(K-Nearest Neighbor)算法的 ...

  4. c++ 枚举简单举例

    #include <iostream> enum Enumeration{ VAL1, VAL2, VAL3=100, VAL4 }; int main() { using namespa ...

  5. hive表操作(转)

    转载于:http://blog.csdn.net/lovelovelovelovelo/article/details/52234971 数据类型 基本数据类型 集合类型,array.map.stru ...

  6. GPIO_F427

  7. Codeforce 1175A From Hero to Zero

    题目链接:http://codeforces.com/problemset/problem/1175/A AC代码: #include<cstdio> #include<iostre ...

  8. k8s 映射 外部服务

    把外部的服务,通过创建service和endpoint,把它映射到k8s内部来使用. 操作步骤: 在10.0.0.13上安装数据库 yum install mariadb-server -y syst ...

  9. linux centos 装g++安装不了

    今天需要编译一个项目的时候在装g++都装不上, [root@master hadoop]# yum install g++ Loaded plugins: fastestmirror, refresh ...

  10. ES6 学习 -- Promise对象

    1.promise含义:可以将promise对象看成是一个容器,它保存着未来才会结束的某个事件(一般是异步操作事件)的结果,各 种异步操作都可以用promise对象来处理promise的特点:(1)p ...