您的位置:首页 > 编程语言 > C语言/C++

C++中的强制类型转换:static_cast,reinterpret_cast,dynamic_cast,const_cast

2019-07-02 21:52 1161 查看

C++中的强制类型转换


原文转自:https://blog.csdn.net/qq_29996285/article/details/86508684

关于本文主题还有下面这篇博客可以参考:https://www.geek-share.com/detail/2726573606.html

tip——string转char*的库函数方法:

string.c_str();
[/code]

 

类型转换

 

静态转换(static_cast)

 

使用方式:

static_cast<目标类型>(原始对象)

用法:

  1. 用于父类和子类之间指针或引用的转换。
  2. 用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证。
  3. 没有父子关系的自定义类型不可以转换

补充:

  • 进行行转换(把派生类的指针或引用转换成基类表示)是安全的;
  • 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。

 

基本数据类型转换的例子:

  1. void test01(){
  2. char a = 'a';
  3. double d = static_cast<double>(a);
  4. cout << "d = " << d <<endl;
  5. }
[/code]

 

继承关系指针互相转换

  1. class Base{};
  2. class Child :public Base{};
  3. class Other{};
  4. void test02(){
  5. Base * base = NULL;
  6. Child * child = NULL;
  7. //把base转为 Child*类型 向下 不安全
  8. Child * child2 = static_cast<Child*>(base);
  9. //把child 转为 Base* 向上 安全
  10. Base * base2 = static_cast<Base*>(child);
  11. //转other类型 转换无效
  12. //Other * other = static_cast<Other*>(base);
  13. }
[/code]

 

继承关系引用相互转换

  1. class Animal{};
  2. class Dog : public Animal{};
  3. class Other{};
  4. void test03(){
  5. Animal ani_ref;
  6. Dog dog_ref;
  7. //继承关系指针转换
  8. Animal& animal01 = ani_ref;
  9. Dog& dog01 = dog_ref;
  10. //子类指针转成父类指针,安全
  11. Animal& animal02 = static_cast<Animal&>(dog01);
  12. //父类指针转成子类指针,不安全
  13. Dog& dog02 = static_cast<Dog&>(animal01);
  14. }
[/code]

 

 

 

动态转换(dynamic_cast)——十分严格

 

使用方式:

dynamic_cast<目标类型>(原始对象)

用法:

  1. 基础类型不可以转换
  2. dynamic_cast主要用于类层次间上行转换和下行转换;
  3. 在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;
  4. 在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全
  5. 发生多态,则允许发生向上转换和向下转换。

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190116161605972.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5OTk2Mjg1,size_16,color_FFFFFF,t_70)

dynamic_cast 如果发生了多态,那么可以让基类转为派生类 ,向下转换

原因:

创建空间大小的时候,是按照子类的大小进行创建的。

使用样例:

  1. class Animal {
  2. public:
  3. virtual void ShowName() = 0;
  4. };
  5. class Dog : public Animal{
  6. virtual void ShowName(){
  7. cout << "I am a dog!" << endl;
  8. }
  9. };
  10. class Other {
  11. public:
  12. void PrintSomething(){
  13. cout << "我是其他类!" << endl;
  14. }
  15. };
  16. //普通类型转换
  17. void test01(){
  18. //不支持基础数据类型
  19. int a = 10;
  20. //double a = dynamic_cast<double>(a);
  21. }
  22. //继承关系指针
  23. void test02(){
  24. Animal* animal01 = NULL;
  25. Dog* dog01 = new Dog;
  26. //子类指针转换成父类指针 可以
  27. Animal* animal02 = dynamic_cast<Animal*>(dog01);
  28. animal02->ShowName();
  29. //父类指针转换成子类指针 不可以
  30. //Dog* dog02 = dynamic_cast<Dog*>(animal01);
  31. }
  32. //继承关系引用
  33. void test03(){
  34. Dog dog_ref;
  35. Dog& dog01 = dog_ref;
  36. //子类引用转换成父类引用 可以
  37. Animal& animal02 = dynamic_cast<Animal&>(dog01);
  38. animal02.ShowName();
  39. }
  40. //无继承关系指针转换
  41. void test04(){
  42. Animal* animal01 = NULL;
  43. Other* other = NULL;
  44. //不可以
  45. //Animal* animal02 = dynamic_cast<Animal*>(other);
  46. }
[/code]

 

 

 

 

常量转换(const_cast)

 

该运算符用来修改类型的const属性

 

使用方式:

  1. 加上const: const int * newP2  =  const_cast<const int *>(p2);
  2. 去掉const:            int * newp    =  const_cast<int *>(p);

用法:

  1. 常量引用被转换成非常量引用,并且仍然指向原来的对象;
  2. 常量指针被转化成非常量指针,并且仍然指向原来的对象;
  3. 不能直接非指针非引用的变量使用const_cast操作符去直接移除它的const.
  1. //常量指针转换成非常量指针
  2. void test01(){
  3. const int* p = NULL;
  4. int* np = const_cast<int*>(p);
  5. int* pp = NULL;
  6. const int* npp = const_cast<const int*>(pp);
  7. const int a = 10; //不能对非指针或非引用进行转换
  8. //int b = const_cast<int>(a); }
  9. //常量引用转换成非常量引用
  10. void test02(){
  11. int num = 10;
  12. int & refNum = num;
  13. const int& refNum2 = const_cast<const int&>(refNum);
  14. }
[/code]

 

 

 

重新解释转换(reinterpret_cast)——最不安全,不推荐

 

主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针.

  1. void test06(){
  2. int a = 10;
  3. int * p = reinterpret_cast<int *>(a);
  4. Base * base = NULL;
  5. Other * other = reinterpret_cast<Other*>(base);
  6. //最不安全 ,不推荐
  7. }
[/code]

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐