参考博文:每日干货丨C语言数据类型转换
参考博文:C语言类型转换时发生了什么?

先回忆一下C语言的强制类型转换格式:


  这种旧式强制类型转换从表现形式上来说不够清晰明了,容易看漏,一旦转换过程出现问题,追踪起来也就更加困难。

为了解决以上问题,C++不仅兼容了C的强制转换,来引入了新的转换方法:


  其中,type是转换的目标类型,exper是要转换的值,cast-name有以下四种:

  ♠\spadesuit♠ static_cast

  ♠\spadesuit♠ const_cast

  ♠\spadesuit♠ dynamic_cast

  ♠\spadesuit♠ reinterpret_cast

一. static_cast

  static_cast 是将表达式转换为某种类型,语法是 T t = static_cast(expression) 。通常用于各种内置类型之间的隐式转换。

例:

分析:
  在上面的示例中,为了更突出地说明问题,我们在用 auto 变量(类型自动推断)来保存转换后的结果,然后输出转换结果的类型。程序输出结果表示,类型转换成功。

附上例代码:

//小问学编程
#include <iostream>
#include<typeinfo>using namespace std;int main() {double a = 1.23;auto b = static_cast<int>(a);cout << b << endl;                  // 1cout << typeid(b).name() << endl;    //  i (i 表示 int)void* p=&a;double* dp = static_cast<double*>(p);cout << p << endl;cout << dp << endl;cout << typeid(p).name() << endl;cout << typeid(dp).name() << endl;cout << *dp << endl;return 0;
}

二. const_cast

  const_cast的作用是修改类型的常量性(const)或易变性(volatile)。通常的用法是将常量转换成非常量,从而可以对这个常量进行修改。const_cast的返回值是一个指向原变量的指针或引用。

  常量指针被转换成非常量指针,并且仍然指向原来的对象;
  常量引用被转换成非常量引用,并且仍然指向原来的对象;

例1:

附例1代码:

//小问学编程
#include <iostream>
#include<typeinfo>using namespace std;int main() {const int a = 1;//a=10;报错,因为a是一个常量auto b = const_cast<int*>(&a);//b是一个非常量指针auto c = const_cast<int&>(a);*b = 2;//a原来地址的数据现在可由*b来改变,解除constc = 3;cout <<"*b = " <<*b<<endl;cout <<" c = " << c <<endl;cout <<" a = " << a <<endl;cout << "type of *b is " << typeid(*b).name()<< endl;cout << "type of  c is " << typeid(c).name() << endl;cout << "type of  a is " << typeid(a).name() << endl;cout << endl;auto* d = const_cast<int*>(&a);*d = 4;cout <<"*d = " <<*d <<endl;cout <<"*b = " <<*b<<endl;cout <<" c = " << c <<endl;cout <<" a = " << a <<endl;//a其实类似 #define a 1 不会改变return 0;
}

例2:

附例2代码:

//小问学编程
#include <iostream>
#include <string>
using namespace std;int main() {const int n = 5;const std::string s = "Inception";std::string& t = const_cast<std::string&>(s); //转换成引用int* k = const_cast<int*>(&n); //转换成指针*k = 6; //转换后指针指向原来的变量t = "Hello World!";cout<<"n: "<<n<<endl;cout<<"s: "<<s<<endl;cout<<"&t: "<<&t<<endl;cout<<"t: "<<t<<endl;cout<<"*k: "<<*k<<endl;cout<<"k: "<<k<<endl;}

例3:常成员函数中去除this指针的const属性

附上例代码:

//小问学编程
#include <iostream>
#include <string>
using namespace std;class CTest
{public:CTest() : m_nTest(2) {}void foo(int nTest) const {//m_nTest = nTest; 错误const_cast<CTest*>(this)->m_nTest = nTest;}int get(){return m_nTest;}public:int m_nTest;
};int main() {CTest t;cout<<"t: "<<t.get()<<endl;t.foo(1);cout<<"t: "<<t.get()<<endl;
}

三. dynamic_cast

  dynamic_cast 用于动态类型转换,只能用于转换含有虚函数的类,用于类层次之间的向上(派生类向基类)和向下转换(基类向派生类)。只能转指针或引用,如果是非法的转换,对于指针返回 NULL ,对于引用抛出异常。
  之所以叫作动态类型转换,就是因为它通过判断在执行到该语句的时候,变量的运行时类型来进行转换的。

例:


  通过示例我们可以看出,基类和派生类之间可以相互转换,但同一基类的不同派生类 (Apple 和 Banana) 之间是不能成功转换的。

附上例代码:

//小问学编程
#include <iostream>
using namespace std;class Fruit {public:virtual void show() {cout << "I am fruit" << endl;}
};class Apple : public Fruit {public:void show() {cout << "I am apple" << endl;}
};class Banana : public Fruit {public:void show() {cout << "I am banana" << endl;}
};int main() {Fruit f;Apple a;Banana b;// 转换成功Fruit* pf1 = dynamic_cast<Fruit*>(&a);a.show();               // I am applepf1->show();            // I am applecout << &a << endl;     // 0x7ffe3298d910cout << pf1 << endl;    // 0x7ffe3298d910Fruit* pf2 = dynamic_cast<Fruit*>(&b);b.show();               // I am bananapf2->show();            // I am bananacout << &b << endl;     // 0x7ffe3298d908cout << pf2 << endl;    // 0x7ffe3298d908// 转换失败Apple* pa = dynamic_cast<Apple*>(&b);cout << &b << endl;     // 0x7ffe3298d908cout << pa << endl;     // 0Banana& pb = dynamic_cast<Banana&>(a);    // terminate called after throwing an instance of 'std::bad_cast'
}

四. reinterpret_cast

  reinterpret_cast 是用来处理无关类型之间的转换。reinterpret_cast 几乎可以转换任意类型,但是很容易出问题,我们尽量不要使用。这里我也不准备花很多篇幅去讲解这个转换,下面用一个例子一笔带过:将一个整数类型转换成指针类型。

例:

  从上面的示例可以看出,我们先将一个指针转换成一个整数,再将这个整数转换成指针,发现两次的指针是相同的。

附上例代码:

//小问学编程
#include <iostream>
using namespace std;int main() {int a  = 1;int* pa = &a;long long int b = reinterpret_cast<long long int>(pa);cout << "pa = " << pa << endl;cout << "b =  " << b << endl;int* c = reinterpret_cast<int*>(b);cout << "c =  " << c << endl;
}

C++之强制转换const_cast、static_cast、dynamic_cast、reinterpret_cast 、dynamic_cast相关推荐

  1. C++四种类型强制转换——const_cast、static_cast、dynamic_cast、reinterpret_cast

    文章目录 一.const_cast 1.基本知识 2.示例演示 二.static_cast 1.基本知识 2.示例演示 三.dynamic_cast 1.基本知识 2.示例演示 四.reinterpr ...

  2. C++的类型强制转换,static_cast,dynamic_cast,const_cast,reinterpret_cast

    1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1type-id(expression)//转换格式2 ...

  3. C++中static_cast/const_cast/dynamic_cast/reinterpret_cast的区别和使用

    C风格的强制转换较简单,如将float a转换为int b,则可以这样:b = (int)a,或者b=int(a). C++类型转换分为隐式类型转换和显示类型转换. 隐式类型转换又称为标准转换,包括以 ...

  4. static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较

    转载于:http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html 隐式转换(implicit conversion) s ...

  5. C/C++编程知识分享:C++四种强制转换,教你多种类型转换方式!

    C语言的强制转换为在数据面前之间加数据类型进行转换,即(目标数据类型)原数据类型.c++为了更好的区分强制转换的类型,达到见其名知其意的效果,共将强制转换分为四种,即 1.static_cast< ...

  6. C++指针类型与强制转换

    C++ 指针类型 指针,用来描述内存地址,并通过提供指针操作来实现与内存相关的程序功能. 指针是一个特殊数据类型,也可以理解为混合数据类型.指针具有数据类型信息及数据在内存空间的地址. C++指针的类 ...

  7. static_cast 与reinterpret_cast

    static_cast 用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保 ...

  8. 【C++ 语言】类型转换 ( 转换操作符 | const_cast | static_cast | dynamic_cast | reinterpret_cast | 字符串转换 )

    文章目录 I . const_cast 转换操作符 II . static_cast 转换操作符 III . dynamic_cast 转换操作符 IV . reinterpret_cast 转换操作 ...

  9. C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast

    一.static_cast:static_cast<new_type> (expression) static_cast相当于传统的C语言里的强制转换,该运算符把expression转换为 ...

最新文章

  1. TVS參数具体解释及选型应用
  2. 备忘录(scanf和continue)
  3. 调查了6万多名开发者后,我们发现了这些...
  4. centos下MySQL Workbench连接时崩溃的解决方法
  5. python键值对储存数据_在python中存储100万个键值对的列表
  6. 遇到问题,尽量自己解决,实在解决不了再问别人
  7. paip.输入法编程---输入法ATIaN历史记录 c823
  8. 有道词典单词本导入到欧路词典单词本
  9. Visualizing and understanding -- 论文阅读笔记
  10. python cad按范围裁剪
  11. python中fn是什么意思_按Fn键Python 3
  12. 百度mip推送工具_链接提交主动推送(实时)、熊掌号天级/周级提交、MIP提交
  13. SAP系统常用配置参数设定
  14. SpringCloud与微服务Ⅷ --- Hystrix断路器
  15. 5.3 背景图层和普通图层的转换 [原创Ps教程]
  16. 不动产测绘数据入库_不动产数据整合中房地信息与各类测绘数据关联入库方法研究...
  17. 高校教材管理系统 数据库
  18. JAVA WEB开发技术作业 个人基本信息表单
  19. Java基础Map循环遍历
  20. 便携计算机主机,一种计算机主机用便携式移动装置及其使用方法与流程

热门文章

  1. Jsp+Ssh+Mysql实现的简单的企业物资信息管理系统
  2. 基于JAVA+SpringBoot+Vue+Mybatis+MYSQL的在线音乐网站
  3. php70w mysql.x8664_LAMP环境的搭建(三)----PHP7的安装
  4. 你竟然在公钥中下毒!——如何在RSA公钥中添加后门
  5. String和StringBuffer的区别
  6. BZOJ2190 SDOI2008 仪仗队
  7. 继续推荐几款VisualStudio的插件
  8. XML Schema --simpleType
  9. sklearn下载方法,如果pycharm下载失败的话,你可以看看这里
  10. ajax引入html_Vue中发送ajax请求的库有哪些?