const_cast使用
来源:互联网 发布:windows 10关闭快捷键 编辑:程序博客网 时间:2024/06/06 02:21
9class B 10 { 11 public: 12 B(int a):val(a) 13 { 14 } 15 16 ~B() 17 { 18 } 19 20 int val; 21 }; 22 23 template<class T> 24 T& remove_const(const T& val) {return const_cast<T&>(val);} 25 26 27 28 class A 29 { 30 public: 31 A() 32 { 33 34 } 35 36 ~A() 37 { 38 39 } 40 41 template<class T> 42 void Put(T& t) 43 { 44 B& b = remove_const(t); 45 b.val = 2; 46 } 47 48 49 private: 50 B* ptr; 51 };
58 int main() 59 { 60 A a; 61 const B b(1); 62 a.Put(b); 63 64 const int i = 2; 65 ++remove_const(i); 66 cout<<i<<endl; 67 cout<<b.val<<endl; 68 return 0; 69 }
类对象可改变值,内置类型无法改变值;
0 0
- const_cast使用
- 被迫使用const_cast
- 使用const_cast的时机
- const_cast的使用
- c++-const_cast 使用
- ststic_cast,const_cast,dynamic_cast区别使用
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- const_cast
- c++ primer
- Android中的windowSoftInputMode属性详解
- 去百度API的百度地图准确叠加和坐标转换的解决方案研究
- 【SICP练习】24 练习1.30
- java 获得用户访问的真实IP地址
- const_cast使用
- QML入门教程
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)
- DSAPI多功能组件编程应用-DS密法
- 【SICP练习】25 练习1.31
- 拾起过去
- Andrew Stankevich's Contest 23
- java 面试 概念点整理
- 【SICP练习】26 练习1.32