【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)的更多相关文章

  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. strlen

    char c1[] = "sdfa";//系统自动添加结束字符 \0 char c2[] = {'1','2','3'};//这样赋值的话,要自己加上结束字符 \0 printf( ...

  2. The Secrets of Oracle Row Chaining and Migration

    from http://www.akadia.com/services/ora_chained_rows.html Overview If you notice poor performance in ...

  3. ionic一些常见问题及方法(网页链接式)

    ionic 进入二级目录以后隐藏底部导航栏(tabs) http://blog.csdn.net/shenshucong520/article/details/48287811ionic三级目录乃至多 ...

  4. linux重置mysql密码

    1.使用重置脚本 wget http://soft.vpser.net/lnmp/ext/reset_mysql_root_password.sh;sh reset_mysql_root_passwo ...

  5. 分享一个移动项目中消除click事件点击延迟的方法

    对于前端工程师来说,apicloud无疑给我们提供了很好的平台,有各种各样的模块供我们使用,但是在实际项目的时候,很大部分的代码,还是需要我们用html css js来实现的.但是呢,移动端页面对于c ...

  6. JBOSS的安全配置 .

    JBoss版本:JBoss 4.2.2.GA jboss默认配置了以下服务:JMX ConsoleJBoss Web Console为了安全起见,需要用户通过授权进行访问. 一.Java 鉴别与授权服 ...

  7. javascript [] 与 {} 的区别

    []是数组形式,{}是对象形式,都可以包含其他类型.如var a= ["A","B",{a:1,b:2}];a[1] 取得的是B,a[2].b取得的是2;var ...

  8. Java基础——数组Array

    一.数组基本概念 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 数组有三 ...

  9. button点击传多个参数

    // --------------------button点击传多个参数------------------------ UIButton *btn = [UIButton buttonWithTyp ...

  10. Jquery点击表格单位时选中其中的Radio的三个方法

    HTML: <table> <tr> <td> 1<br> <input type="radio" name="ch ...