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相互转换 用于基类和子类之间的指针和引用 ...
随机推荐
- 阿里云+wordpress搭建个人博客网站
[正文] 在阿里云上搭建使用个人博客主要分为以下几个步骤: 1.购买阿里云ECS主机 2.购买域名 3.申请备案 4.环境配置 5.安装wordpress 6.域名解析 声明一下,本人对服务器端的知识 ...
- 记录---base64
什么是Base64呢? Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045-RFC2049,上面有MIME的详细规范.Base64编码可用于在HTTP环境下 ...
- 使用git命令提交远程github仓库的时候提示"rejected"(拒绝)解决办法
今天使用在自己笔记本本地磁盘上使用Git命令行执行"git push"然后输入github的用户名和密码之后,报如下错误: ![rejected] master->maste ...
- python语法笔记(二)
1. 循环对象 循环对象是一类特殊的对象,它包含一个next()方法(在python3中是 __next__()方法),该方法的目的是进行到下一个结果,而在结束一系列结果之后,举出 StopItera ...
- JSON字符串如何转化成对象?
解析 1.定义:是指将符合 JSON 语法规则的字符串转换成对象的过程. 2.不同的编程语言都提供了解析 JSON 字符串的方法,在这里主要讲解 JavaScript 中的解析方法.主要有三种: 1) ...
- 【转】 SIFT算法详解
尺度不变特征变换匹配算法详解Scale Invariant Feature Transform(SIFT)Just For Fun zdd zddmail@gmail.com 对于初学者,从Davi ...
- ACM2 递归 n分成k份
//将n 分成k份的 分法总数 #include "stdafx.h" #include"stdio.h" #include<iostream> u ...
- 如何在Objective-C中实现链式语法
在接触到开源项目 Masonry 后,里面的布局约束的链式写法让我颇感兴趣,就像下面这样: 1 2 3 4 5 6 7 8 UIEdgeInsets padding = UIEdgeInsetsMak ...
- Firefox火狐Flash插件卡死问题完美解决方法(转载)
http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...
- Football(POJ3071)
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3469 Accepted: 1782 Descript ...