强制类型转换(const_cast)
【1】 const_cast的作用
一、常量指针 被强转为 非常量指针,且仍然指向原来的对象;
二、常量引用 被强转为 非常量引用,且仍然指向原来的对象;
三、常量对象 被强转为 非常量对象。
【2】 实例代码
代码如下:
- #include <iostream>
- using namespace std;
- const int xx = ;
- class A
- {
- public:
- int m_nNum;
- public:
- A(int nValue = );
- };
- A::A(int nValue) : m_nNum(nValue)
- {
- }
- void TestFun()
- {
- // 第一种情况: const修饰指针指向对象
- const A *pA = new A();
- // pA->m_nNum = 100; // compile error ! pA指针指向的对象为常对象,其成员变量值为只读的。
- A* pAA = const_cast<A*>(pA); // 去掉pA指针的const属性
- pAA->m_nNum = ; // pAA指针指向的对象为一般对象,其成员变量值可读写。
- cout << pA->m_nNum << endl; // 199
- // 第二种情况: const修饰指针
- A *pB = new A();
- pA = pB; // 思考这个原因。为什么这样子可以呢?且再看下面的这种情况:
- A* const pC = new A();
- cout << pC->m_nNum << endl; //
- A *pD = new A();
- // pC = pD; // compile error ! pC指针变量被const修饰,其值是只读的。
- A*& pE = const_cast<A*>(pC); // 去掉pC指针变量的const属性。再赋给指针引用变量
- pE = pD;
- cout << pC->m_nNum << endl; //
- A* pAS = const_cast<A*>(pC); // 去掉pC指针变量的const属性。再赋给一般指针变量
- pAS->m_nNum = ; // 通过去掉const属性的指针变量修改其成员变量值
- cout << pC->m_nNum << endl; // 3
- // 第三种情况:const修饰指针和指针对象
- const A* const pCC = new A();
- const A* pCC2 = const_cast<A*>(pCC);
- // pCC2->m_nNum = 119; // error C3490: 由于正在通过常量对象访问“m_nNum”,因此无法对其进行修改
- pCC2 = NULL;
- A* const pCC3 = const_cast<A*>(pCC);
- pCC3->m_nNum = ;
- // pCC3 = NULL; error C3892: “pCC3”: 不能给常量赋值
- A* pCC4 = const_cast<A*>(pCC);
- pCC4->m_nNum = ;
- pCC4 = NULL;
- // 第四种情况:const修饰对象,去const属性后赋给一般对象
- const A a;
- // a.m_nNum = 101; // compile error ! 常对象具有只读属性。
- A b = const_cast<A&>(a);
- b.m_nNum = ;
- cout << a.m_nNum << endl; //
- cout << b.m_nNum << endl; // 101
- // 第五种情况:const修饰对象,去const属性后赋给引用对象
- const A c;
- // c.m_nNum = 101; // compile error ! 常对象具有只读属性。
- A& d = const_cast<A&>(c);
- d.m_nNum = ;
- cout << c.m_nNum << endl; //
- cout << d.m_nNum << endl; // 102
- // 第六种情况:const修饰对象,对象指针去const属性后赋给指针
- const A e;
- // e.m_nNum = 103; // compile error ! 常对象具有只读属性。
- A* pe = const_cast<A*>(&e);
- pe->m_nNum = ;
- cout << e.m_nNum << endl; //
- cout << pe->m_nNum << endl; // 103
- // 第七种情况:const修饰局部变量
- const int xx = ;
- int* yy = const_cast<int *>(&xx);
- *yy = ;
- cout << xx << endl; //
- cout << *yy << endl; //
- int aa = xx;
- cout << aa << endl; // 50
- // 第八种情况:const修饰局部变量。去const属性后赋给一般变量
- const int xxx = ;
- int yyy = const_cast<int&>(xxx);
- yyy = ;
- cout << xxx << endl; //
- cout << yyy << endl; // 51
- // 第九种情况:const修饰局部变量。去const属性后赋给引用变量
- const int xxx2 = ;
- int& yyy2 = const_cast<int&>(xxx2);
- yyy2 = ;
- cout << xxx2 << endl; //
- cout << yyy2 << endl; //
- }
- void main()
- {
- TestFun();
- system("pause");
- }
- // run out:
- /*
- 199
- 1
- 2
- 3
- 100
- 101
- 102
- 102
- 103
- 103
- 50
- 200
- 50
- 50
- 51
- 50
- 52
- 请按任意键继续. . .
- */
Good Good Study, Day Day Up.
顺序 选择 循环 总结
强制类型转换(const_cast)的更多相关文章
- C++强制类型转换操作符 const_cast
const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转 ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
- C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别
C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...
- C++强制类型转换
C语言强制类型转换过于粗暴,任意类型之间都可以进行转换,编译很难判断其正确性; 难于定位,在源码中无法快速定位所有使用强制类型转换的语句. C++将强制类型转换分为4种不同的类型:static_cas ...
- C++强制类型转换操作符 static_cast
static_cast是一个强制类型转换操作符.强制类型转换,也称为显式转换,C++中强制类型转换操作符有static_cast.dynamic_cast.const_cast.reinterpert ...
- C++的几种强制类型转换
有时我们希望显式地将对象强制类型转换成另外一种类型.例如,如果想在下面的代码中执行浮点数除法: int i, j; double slope = i / j; 就要使用某种方法将i和/或j显式地转换成 ...
- C++开发必看 四种强制类型转换的总结 [转]
一.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a 二.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. co ...
随机推荐
- MVC中@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName的区别
MVC中,在Controllers查询到数据,返回一个实体给View并显示,可以用@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName ...
- dede如何实现二级栏目导航的仿制
{dede:channelartlist row='2' typeid='1,2' }<h3><a href='{dede:field name='typeurl'/}'>{d ...
- mysql 恢复
一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...
- webconfig和appconfig中出现特殊字符如何处理
在配置文件出现特殊字符&的字符串(如:abce&efg),就会报错.错误如下: 如何处理呢? config文件的本质是xml,所以必须符合xml的规范我们需要这么处理: abce&am ...
- C# DevExpress_gridControl 行号行样式
#region 行号 /// <summary> /// 行号 /// </summary> /// <param name="sender"> ...
- iOS 对UIButton的imageView和titleLabel进行重新布局
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- C# MVC 实现登录的5种方式
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷. 学无止境,精益求精 小弟之前做过三月的MVC,后来又一直webFo ...
- Java基础之集合框架——使用HashMap地图(TryPhoneBook1)
控制台程序. 首先改进Peron类,使Person可以在地图中用作键,进而存储电话簿中的项.必须添加equals()方法并重写默认的hashCode()方法. import java.io.*; pu ...
- java 笔记(4) —— java I/O 流、字节流、字符流
Java中使用流来处理程序的输入和输出操作,流是一个抽象的概念,封装了程序数据于输入输出设备交换的底层细节.JavaIO中又将流分为字节流和字符流,字节流主要用于处理诸如图像,音频视频等二进制格式数据 ...
- Leetcode: String to Integer
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...