C++中const-cast与reinterpret-cast运算符的用法
C++中const_cast与reinterpret_cast运算符的用法,经常被用于表达式中的类型转换,下面是小编分享的运算符的用法,一起来看一下吧。
const_cast 运算符
从类中移除 const、volatile 和 __unaligned 特性。
const_cast <
type-id
> (
expression
)
备注
指向任何对象类型的指针或指向数据成员的指针可显式转换为完全相同的类型(const、volatile 和 __unaligned 限定符除外)。对于指针和引用,结果将引用原始对象。对于指向数据成员的指针,结果将引用与指向数据成员的原始(未强制转换)的指针相同的成员。根据引用对象的类型,通过生成的指针、引用或指向数据成员的.指针的写入操作可能产生未定义的行为。
您不能使用 const_cast 运算符直接重写常量变量的常量状态。
const_cast 运算符将 null 指针值转换为目标类型的 null 指针值。
// expre_const_cast_
// compile with: /EHsc
#include <iostream>
using namespace std;
class CCTest {
public:
void setNumber( int );
void printNumber() const;
private:
int number;
};
void CCTest::setNumber( int num ) { number = num; }
void CCTest::printNumber() const {
cout << "Before: " << number;
const_cast< CCTest * >( this )->number--;
cout << "After: " << number;
}
int main() {
CCTest X;
umber( 8 );
tNumber();
}
在包含 const_cast 的行中,this 指针的数据类型为 const CCTest *。 const_cast 运算符会将 this 指针的数据类型更改为 CCTest *,以允许修改成员 number。强制转换仅对其所在的语句中的其余部分持续。
reinterpret_cast 运算符
允许将任何指针转换为任何其他指针类型。也允许将任何整数类型转换为任何指针类型以及反向转换。
语法
reinterpret_cast < type-id > ( expression )
备注
滥用 reinterpret_cast 运算符可能很容易带来风险。除非所需转换本身是低级别的,否则应使用其他强制转换运算符之一。
reinterpret_cast 运算符可用于 char* 到 int* 或 One_class* 到 Unrelated_class* 之类的转换,这本身并不安全。
reinterpret_cast 的结果不能安全地用于除强制转换回其原始类型以外的任何用途。在最好的情况下,其他用途也是不可移植的。
reinterpret_cast 运算符不能丢掉 const、volatile 或 __unaligned 特性。有关移除这些特性的详细信息,请参阅 const_cast Operator。
reinterpret_cast 运算符将 null 指针值转换为目标类型的 null 指针值。
reinterpret_cast 的一个实际用途是在哈希函数中,即,通过让两个不同的值几乎不以相同的索引结尾的方式将值映射到索引。
#include <iostream>
using namespace std;
// Returns a hash code based on an address
unsigned short Hash( void *p ) {
unsigned int val = reinterpret_cast<unsigned int>( p );
return ( unsigned short )( val ^ (val >> 16));
}
using namespace std;
int main() {
int a[20];
for ( int i = 0; i < 20; i++ )
cout << Hash( a + i ) << endl;
}
Output:
64641
64645
64889
64893
64881
64885
64873
64877
64865
64869
64857
64861
64849
64853
64841
64845
64833
64837
64825
64829
reinterpret_cast 允许将指针视为整数类型。结果随后将按位移位并与自身进行“异或”运算以生成唯一的索引(具有唯一性的概率非常高)。该索引随后被标准 C 样式强制转换截断为函数的返回类型。
-
C语言猴子吃桃问题
猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?分析:1)设x1为前一天桃子数,设x2为第二天桃子数,则:x...
-
关于C语言精选模拟练习题
C语言是一个有结构化程序设计、具有变量作用域(variablescope)以及递归功能的过程式语言。下面是小编为大家整理的关于C语言精选模拟练习题,欢迎参考~5:请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用c语言提供的`求字符串长度的函数),函数返回较长...
-
在C语言中转换时间的基本方法
本文主要介绍了在C语言中转换时间的基本方法,分别是mktime()函数和localtime()函数的使用,供参考学习,感兴趣的小伙伴们可以参考一下!想了解更多相关信息请持续关注我们应届毕业生考试网!C语言mktime()函数:将时间转换成经过的秒数头文件:#include<time.h>定义函数:t...
-
C语言与JAVA的区别
很多人不明白C语言与C++和JAVE之间的区别,为此,为大家收集整理了下面的内容,欢迎阅读!C语言C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语...
相关文章
- A Letter to Univsity Pesident about the Canteen Service on C
- Protecting water resources英语作文
- script runat server 在ASP.NET中的用法
- The relationship between theory and practice大学英语作文
- Certificate Craze on Campus英语四级作文
- 四级作文:Create a Conservation-Oriented Campus
- 分享SAT阅读练习题:Reading Comprehension Test 14
- 英语阅读:VIT declines to acquire Ang Mo Kio property
- 我的英语演讲稿living at present is my top concern
- 托福英语写作范文:Examinations exert a pernicious influence on educat