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. post请求传文件

    public static JSONObject doFormDataPost(File file, String sURL) throws IOException { HttpClient cont ...

  2. bash数组总结

    bash数组操作 bash支持两种数组,一种是索引数组,一种是关联数组 索引数组 数组的值类型是任意的,索引也未必一定要连续,当做列表理解更好 下面总结下索引数组,即列表: 1. 声明 declare ...

  3. jquery中typeof的用法

    typeof 可以用来检测给定变量的数据类型,可能的返回值: 'undefined' 'boolean' 'string' 'number' 'object' 'function' var hahah ...

  4. 函数的属性和方法, apply和call的区别及bind的使用

    ==>我的新博客中 http://www.suanliutudousi.com/2017/08/27/%E5%87%BD%E6%95%B0%E7%9A%84%E5%B1%9E%E6%80%A7% ...

  5. 城市代码表mysql

    只有代码: # ************************************************************ # Sequel Pro SQL dump # Version ...

  6. Linux 常用命令:开发调试篇

    前言 Linux常用命令中有一些命令可以在开发或调试过程中起到很好的帮助作用,有些可以帮助了解或优化我们的程序,有些可以帮我们定位疑难问题.本文将简单介绍一下这些命令. 示例程序 我们用一个小程序,来 ...

  7. Linux 服务器 个人常用操作命令记录

    1.实时查看log:tail -f 日志文件名 2.查看Apache运行的用户组或用户名:ps aux | grep httpd 或者是: ps -ef | grep httpd 3.查看cronta ...

  8. Java 几种队列区别的简单说明

    前言 队列,字面意思就可以明白. 是一种线性的数据暂存与管理工具. 也可以让各种业务功能进行逐个的队列运行. 此篇博客只说明一下Java有几种队列 未阻塞和阻塞队列的区别 未阻塞: 1.未阻塞的队列在 ...

  9. 本地git安装完成之后,从远程git服务器上面下载代码。报错SSL certificate problem:self signed certificate in certificate chain。

    解决方案:打开git的控制端黑窗口,输入: git config --global http.sslVerify false 点击Entry之后,就会去掉git的ssl验证. 然后就可以正常的下载代码 ...

  10. thinkphp empty标签

    empty标签用于判断某个变量是否为空,用法: 大理石平台检验标准 <empty name="name"> name为空值 </empty> 如果判断没有赋 ...