首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++中四种类型转换以及const_cast是否能改变常量的问题

C++中四种类型转换以及const_cast是否能改变常量的问题

作者头像
s1mba
发布2018-01-03 20:24:13
1.3K0
发布2018-01-03 20:24:13
举报
文章被收录于专栏:开发与安全开发与安全

we have four specific casting operators:dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.

dynamic_cast <new_type> (expression) reinterpret_cast <new_type> (expression) static_cast <new_type> (expression) const_cast <new_type> (expression)

一、对C++中四种类型转换总结如下:

const_cast<T>(expr) 用来移除对象的常量性(cast away the constness) const_cast一般用于指针或者引用 使用const_cast去除const限定的目的不是为了修改它的内容 使用const_cast去除const限定,通常是为了函数能够接受这个实际参数 static_cast<T>(expr) 编译器隐式执行的任何类型转换都可以由static_cast完成 当一个较大的算术类型赋值给较小的类型时,可以用static_cast进行强制转换。

可以将void*指针转换为某一类型的指针

可以将基类指针强制转换为派生类指针,但是不安全。 无法将const转化为nonconst,这个只有const_cast才可以办得到 reinterpret_cast<T>(expr)  “通常为操作数的位模式提供较低层的重新解释”也就是说将数据以二进制存在形式的重新解释。 int i; char *p = "This is a example."; i = reinterpret_cast<int>(p); //此时结果,i与p的值是完全相同的。 int *ip char *pc = reinterpret_cast<char*>(ip); // 程序员需要记得pc所指向的真实对象是int型,并非字符串。 // 如果将pc当作字符指针进行操作,可能会造成运行时错误 // 如int len = strlen(pc); 多重继承时reinterpret_cast不安全。static_cast会根据父子类指针转换的偏移量转换到正确地址,而reinterpret_cast不会。

#include <cassert>
#include <iostream>
using namespace std;

class A
{
public:
    int m_a;
};

class B
{
public:
    int m_B;
};

class C: public A, public B
{

};

int main(void)
{

    C c;
    cout << (void *)&c << endl;
    cout << (void *)reinterpret_cast<B *>(&c) << endl;

    cout << (void *)static_cast<B *>(&c) << endl;

    return 0;
}

dynamic_cast<T>(expr) 执行“安全向下”转型操作,也就是说支持运行时识别指针或所指向的对象,这是唯一个无法用旧式语来进行的转型操作。

dynamic_cast可谓是最严格的转换,static_cast次之,而reinterpret_cast则是最宽松的。如果你遇到不能将整型转变为函数指针的问题,你可以这样解决: reinterpret_cast<LPFUN&>(nAddress); 注意LPFUN这里有个“&”符号,表示引用,C++的引用其实就是用指针实现的,而这些“转换”其实都是指针的转换,所以加上引用符号编译才能通过。

二、也许大家都有过这样的疑惑:const_cast可以去除一个常量的const属性,去除const属性后应该可以对“常量”进行修改,通过调试器发现内存中的值是被改变的,可是再传递这个“常量”的时候,值却一直保持原状,实在古怪,在Windows下用VC、尝试如此,在Linux下用g++尝试也如此,我原先以为和编译器的优化选项有关系,把所有优化选项关闭,照样没用,为什么?

写了个程序进行测试:

#include <iostream>
using namespace std;

void Fun(int &value)
{
    cout << "Fun(val) = " << value << endl;
}

int main(void)
{
    const int val = 100;

    int *ptr = const_cast<int *>(&val);
    *ptr = 200;
    cout << &val << endl;
    cout << ptr << endl;
    cout << "val = " << val << endl;
    cout << "*ptr = " << *ptr << endl;


    int &ref = const_cast<int &>(val);
    ref = 300;
    cout << "val = " << val << endl;
    cout << "ref = " << ref << endl;

    Fun(const_cast<int &>(val));

    return 0;
}

输出为:

可以看出打印的地址是一样的,而且奇怪的是val还是等于100,而通过*ptr打印出来的却是更改后的200,再者Fun函数打印的是300,即被引用再次修改了一次,在打印语句附近设置断点并调试反汇编,截取一段如下图:

可以明显地看出系统是对val这个const进行了预处理般的替换,将它替换成“64h”(十六进制的64就是十进制的100),即在编译生成的指令中val就已经被替换成100了,其实加const只是告诉编译器不能修改而不是真正地不可修改,如果程序员不注意而去修改了它会报错,现在我们利用const_cast去除了常量性,然后通过指针和引用对其进行了修改,所以通过指针打印或者引用传参的时候就能看出其内存确实变化了,但为了保护val这个变量本来的const特性,所以每次我们使用val时,系统都将其替换成初始值100,确保了val还是“不可变”的。记住,只有当const限定符加在已经初始化的全局变量前面的时候,此时变量处在.rodata段(linux下),才是真正的不可修改,否则通过指针都是可以修改的,虽然编译过程中会产生警告。

在linux下测试也是同样的输出结果:

附录:

MSDN上关于四种cast的说明:

reinterpret_cast Operator

The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators.

dynamic_cast Operator

The expression dynamic_cast( expression ) converts the operand expression to an object of type type-id. The type-id must be a pointer or a reference to a previously defined class type or a“pointer to void”. The type of expression must be a pointer if type-id is a pointer, or an l-value if type-id is a reference.

static_cast Operator

The expression static_cast < type-id > ( expression ) converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.

const_cast operator

The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.

参考:http://blog.csdn.net/guogangj/archive/2007/03/29/1545119.aspx

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-04-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档