C++-const_cast, reinterpret_cast, static_cast的用法
///////////////////////////////////////////////////////////////////////////////
//
// FileName : cast_item27.cpp
// Version : 0.10
// Author : Ryan Han
// Date : 2013/10/31 15:43:55
// Comment :
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std; int main() {
//static_cast
int a = ;
a = ;
const int b = static_cast<const int>(a);
cout << "b is: " << b << endl; //dynamic_cast
//see D:\cygwin\home\baoweih\code_book\c++ primer\p838_dynamiccast.h //reinterpret_cast
char* k = reinterpret_cast<char*>(b); //const_cast
//http://www.cnblogs.com/dracohan/p/3417842.html
const int* i = &b;
//compile error, invalid conversion from ¡®const int*¡¯ to ¡®int*¡¯
//int* j = i;
int* j = const_cast<int*>(i);
//successfully change a const value
*j = ;
cout << "b is: " << b << endl; //reference
const int& l = b;
int& m = const_cast<int&>(l);
//error: assignment of read-only reference ¡®l¡¯
//l = 7;
//successfully change a const reference
m = ;
cout << "b is: " << b << endl; //const point
const int* const pint = new int();
// can't change *pint
//*pint = 1023;
// cant' change pint also
//pint = new int(1023);
int* const fake_pint = const_cast<int* const>(pint);
// can change *pint now
*fake_pint = ;
// still cant' change pint also
//fake_pint = new int(1023); cout << "*fake_pint is: " << *fake_pint << endl; return ;
}
C++-const_cast, reinterpret_cast, static_cast的用法的更多相关文章
- dynamic_cast,const_cast,static_cast,reinterpret_cast 详解
如果直接指针直接强转,将只能访问虚函数的内容,而不能访问特定类中的特定成员或方法!!!! 强制类型转换运算符:C++有四种强制类型转换符,分别是dynamic_cast,const_cast,stat ...
- C++中四种类型转换方式(ynamic_cast,const_cast,static_cast,reinterpret_cast)
Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
- C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast
关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...
- const_cast, dynamic_cast, static_cast,reinterpret_cast
一.const_cast:用于移除const数据,目标数据类型必须与原类型相同 二.dynamic_cast:用于在两个不同类型之间进行强制转换并且在执行运行时检查它.保证它的合法性,如果在两个互相矛 ...
- C++中reinterpret_cast、const_cast、static_cast、dynamic_cast的作用与区别
1.reinterpret_cast 作用及原理:将一个类型的指针,转换为另一个类型的指针,这种转换不用修改指针变量值数据存放格式(不改变指针变量值),只需在编译时重新解释指针的类型就可以,当然他也可 ...
- C++的四种转换(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
static_cast 相当于C语言中的强制转换:(类型)表达式或类型(表达式),用于各种隐式转换 非const转const.void*转指针.int和char相互转换 用于基类和子类之间的指针和引用 ...
随机推荐
- Java编程思想学习笔记_4(异常机制,容器)
一.finally语句注意的细节: 当涉及到break和continue语句的时候,finally字句也会得到执行. public class Test7 { public static void m ...
- 如何判断一个GPS点是否在以另一个GPS点为圆心100米为半径的圆内(Java代码)
题目乍一看,无从下手,仔细想了一下,原来只需要判断两个GPS点的直线距离是否<100米即可. Java代码如下: /** * 将两个经纬度坐标转化成距离(米) * * @param 2个GPS经 ...
- php学习记录 易混淆
1.echo和print的区别 PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.echo输出后没有返回值,但print有返回值,当其执行失败时返回flase.因此可以 ...
- LayoutParams使用
LayoutParams继承于Android.View.ViewGroup.LayoutParams. LayoutParams相当于一个Layout的信息包,它封装了Layout的位置. ...
- COM组件(ATL篇)
目录 第1章创建进程内组件 1 1.1 目标 1 1.2 创建项目 3 1.2.1 VC++6.0 3 1.2.2 VC++2010 5 1.3 增加COM类 6 ...
- android GC内存回收小析
由于时间问题,简单的谈谈自己的理解. 大家都知道,在android开发中,不需要自己去管理,有垃圾回收机制会自动帮我们去回收 没有被引用到的对象. 那垃圾回收机制到底是怎样的呢?下面列出本人的一些理解 ...
- linux笔记:linux常用命令-网络命令
网络命令:ping(测试网络连通性) 网络命令:ifconfig(查看和设置网卡信息) 注意:在查看网卡信息时,直接输入ifconfig命令即可. 网络命令:last(列出目前和过去登入系统的用户信息 ...
- linux笔记:linux帮助命令,man,help,whatis,apropos
命令名称:man功能:获得帮助信息命令所在路径:/usr/bin/man用法:man 命令或配置文件其他:会调用less来查看该命令或配置文件的帮助信息. 命令名称:whatis功能:获得命令的简短介 ...
- 【bzoj3160】【xsy1726】万径人踪灭
[bzoj3160]万径人踪灭 题意 给定一个由'a'和'b'构成的字符串,求不连续回文子序列的个数. \(n\leq 100000\) 分析 还是蛮不错的. 这道题基本上是自己想到的. 除了没有利用 ...
- Bash脚本15分钟进阶教程
转载: Bash脚本15分钟进阶教程 这里的技术技巧最初是来自谷歌的"Testing on the Toilet" (TOTT).这里是一个修订和扩增版本. 脚本安全 我的所有ba ...