强制类型转换(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 ...
随机推荐
- http://blog.csdn.net/yunhua_lee/article/details/52710894
http://blog.csdn.net/yunhua_lee/article/details/52710894
- 微信小店 API 手册
微信商铺API手册V1.13 目录 1. 商品管理接口.................................................................... ...
- H3C Series Router MSR26-00与F3736 VPN IP SEC
注:建立链接之后经常断线,需要两边进行PING通才可以.待解决.
- python 模块zlib 压缩与解压
例子1:压缩与解压字符串 import zlib message = 'abcd1234' compressed = zlib.compress(message) decompressed = zli ...
- D3D9 GPU Hacks (转载)
D3D9 GPU Hacks I’ve been trying to catch up what hacks GPU vendors have exposed in Direct3D9, and tu ...
- ORA-16179: incremental changes to "log_archive_dest_1" not allowed with SPFILE
SQL> alter system set log_archive_dest_1='E:\arch ' scope=both; alter system set log_archive_dest ...
- j2ee ehcache
Ehcache is an open source, standards-based cache that boosts performance, offloads your database, an ...
- Groupon面经Prepare: Max Cycle Length
题目是遇到偶数/2,遇到奇数 *3 + 1的题目,然后找一个range内所有数字的max cycle length.对于一个数字,比如说44,按照题目的公式不停计算,过程是 44, 22, 11, 8 ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- HTML--表单,图片热点,网页划区和拼接
一.图片热点 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: <img src="/ usemap="longzhu"> ...