【1】 const_cast的作用

一、常量指针 被强转为 非常量指针,且仍然指向原来的对象;

二、常量引用 被强转为 非常量引用,且仍然指向原来的对象;

三、常量对象 被强转为 非常量对象。

【2】 实例代码

代码如下:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int xx = ;
  5.  
  6. class A
  7. {
  8. public:
  9. int m_nNum;
  10.  
  11. public:
  12. A(int nValue = );
  13. };
  14.  
  15. A::A(int nValue) : m_nNum(nValue)
  16. {
  17. }
  18.  
  19. void TestFun()
  20. {
  21. // 第一种情况: const修饰指针指向对象
  22. const A *pA = new A();
  23. // pA->m_nNum = 100; // compile error ! pA指针指向的对象为常对象,其成员变量值为只读的。
  24. A* pAA = const_cast<A*>(pA); // 去掉pA指针的const属性
  25. pAA->m_nNum = ; // pAA指针指向的对象为一般对象,其成员变量值可读写。
  26. cout << pA->m_nNum << endl; // 199
  27.  
  28. // 第二种情况: const修饰指针
  29. A *pB = new A();
  30. pA = pB; // 思考这个原因。为什么这样子可以呢?且再看下面的这种情况:
  31. A* const pC = new A();
  32. cout << pC->m_nNum << endl; //
  33. A *pD = new A();
  34. // pC = pD; // compile error ! pC指针变量被const修饰,其值是只读的。
  35.  
  36. A*& pE = const_cast<A*>(pC); // 去掉pC指针变量的const属性。再赋给指针引用变量
  37. pE = pD;
  38. cout << pC->m_nNum << endl; //
  39.  
  40. A* pAS = const_cast<A*>(pC); // 去掉pC指针变量的const属性。再赋给一般指针变量
  41. pAS->m_nNum = ; // 通过去掉const属性的指针变量修改其成员变量值
  42. cout << pC->m_nNum << endl; // 3
  43.  
  44. // 第三种情况:const修饰指针和指针对象
  45. const A* const pCC = new A();
  46. const A* pCC2 = const_cast<A*>(pCC);
  47. // pCC2->m_nNum = 119; // error C3490: 由于正在通过常量对象访问“m_nNum”,因此无法对其进行修改
  48. pCC2 = NULL;
  49. A* const pCC3 = const_cast<A*>(pCC);
  50. pCC3->m_nNum = ;
  51. // pCC3 = NULL; error C3892: “pCC3”: 不能给常量赋值
  52. A* pCC4 = const_cast<A*>(pCC);
  53. pCC4->m_nNum = ;
  54. pCC4 = NULL;
  55.  
  56. // 第四种情况:const修饰对象,去const属性后赋给一般对象
  57. const A a;
  58. // a.m_nNum = 101; // compile error ! 常对象具有只读属性。
  59. A b = const_cast<A&>(a);
  60. b.m_nNum = ;
  61. cout << a.m_nNum << endl; //
  62. cout << b.m_nNum << endl; // 101
  63.  
  64. // 第五种情况:const修饰对象,去const属性后赋给引用对象
  65. const A c;
  66. // c.m_nNum = 101; // compile error ! 常对象具有只读属性。
  67. A& d = const_cast<A&>(c);
  68. d.m_nNum = ;
  69. cout << c.m_nNum << endl; //
  70. cout << d.m_nNum << endl; // 102
  71.  
  72. // 第六种情况:const修饰对象,对象指针去const属性后赋给指针
  73. const A e;
  74. // e.m_nNum = 103; // compile error ! 常对象具有只读属性。
  75. A* pe = const_cast<A*>(&e);
  76. pe->m_nNum = ;
  77. cout << e.m_nNum << endl; //
  78. cout << pe->m_nNum << endl; // 103
  79.  
  80. // 第七种情况:const修饰局部变量
  81. const int xx = ;
  82. int* yy = const_cast<int *>(&xx);
  83. *yy = ;
  84. cout << xx << endl; //
  85. cout << *yy << endl; //
  86. int aa = xx;
  87. cout << aa << endl; // 50
  88.  
  89. // 第八种情况:const修饰局部变量。去const属性后赋给一般变量
  90. const int xxx = ;
  91. int yyy = const_cast<int&>(xxx);
  92. yyy = ;
  93. cout << xxx << endl; //
  94. cout << yyy << endl; // 51
  95.  
  96. // 第九种情况:const修饰局部变量。去const属性后赋给引用变量
  97. const int xxx2 = ;
  98. int& yyy2 = const_cast<int&>(xxx2);
  99. yyy2 = ;
  100. cout << xxx2 << endl; //
  101. cout << yyy2 << endl; //
  102. }
  103.  
  104. void main()
  105. {
  106. TestFun();
  107. system("pause");
  108. }
  109.  
  110. // run out:
  111. /*
  112. 199
  113. 1
  114. 2
  115. 3
  116. 100
  117. 101
  118. 102
  119. 102
  120. 103
  121. 103
  122. 50
  123. 200
  124. 50
  125. 50
  126. 51
  127. 50
  128. 52
  129. 请按任意键继续. . .
  130. */

Good Good Study, Day Day Up.

顺序 选择 循环 总结

强制类型转换(const_cast)的更多相关文章

  1. C++强制类型转换操作符 const_cast

    const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转 ...

  2. c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast

    c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构  dynamic_ca ...

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

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

  4. 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...

  5. C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别

    C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...

  6. C++强制类型转换

    C语言强制类型转换过于粗暴,任意类型之间都可以进行转换,编译很难判断其正确性; 难于定位,在源码中无法快速定位所有使用强制类型转换的语句. C++将强制类型转换分为4种不同的类型:static_cas ...

  7. C++强制类型转换操作符 static_cast

    static_cast是一个强制类型转换操作符.强制类型转换,也称为显式转换,C++中强制类型转换操作符有static_cast.dynamic_cast.const_cast.reinterpert ...

  8. C++的几种强制类型转换

    有时我们希望显式地将对象强制类型转换成另外一种类型.例如,如果想在下面的代码中执行浮点数除法: int i, j; double slope = i / j; 就要使用某种方法将i和/或j显式地转换成 ...

  9. C++开发必看 四种强制类型转换的总结 [转]

    一.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:     TYPE b = (TYPE)a 二.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. co ...

随机推荐

  1. MVC中@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName的区别

    MVC中,在Controllers查询到数据,返回一个实体给View并显示,可以用@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName ...

  2. dede如何实现二级栏目导航的仿制

    {dede:channelartlist row='2' typeid='1,2' }<h3><a href='{dede:field name='typeurl'/}'>{d ...

  3. mysql 恢复

    一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...

  4. webconfig和appconfig中出现特殊字符如何处理

    在配置文件出现特殊字符&的字符串(如:abce&efg),就会报错.错误如下: 如何处理呢? config文件的本质是xml,所以必须符合xml的规范我们需要这么处理: abce&am ...

  5. C# DevExpress_gridControl 行号行样式

    #region 行号 /// <summary> /// 行号 /// </summary> /// <param name="sender"> ...

  6. iOS 对UIButton的imageView和titleLabel进行重新布局

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  7. C# MVC 实现登录的5种方式

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    小弟之前做过三月的MVC,后来又一直webFo ...

  8. Java基础之集合框架——使用HashMap地图(TryPhoneBook1)

    控制台程序. 首先改进Peron类,使Person可以在地图中用作键,进而存储电话簿中的项.必须添加equals()方法并重写默认的hashCode()方法. import java.io.*; pu ...

  9. java 笔记(4) —— java I/O 流、字节流、字符流

    Java中使用流来处理程序的输入和输出操作,流是一个抽象的概念,封装了程序数据于输入输出设备交换的底层细节.JavaIO中又将流分为字节流和字符流,字节流主要用于处理诸如图像,音频视频等二进制格式数据 ...

  10. Leetcode: String to Integer

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...