由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个诡异的const_cast问题
相关主题
C++ 的 问题question about const reference
conversion between const to nonconstc++环境入门问题
关于const_cast,地址一样,值不同?这个地址咋回事?
问题: C++ static_cast between int and float面试问题
question for C++ constant请教一个c++ reference问题
C++菜问: 怎么这样也可以?题2
post incrementUndefined symbols for architecture x86_64: 求助
有关objec access path的问题g++ default optimization error
相关话题的讨论汇总
话题: const话题: cast话题: endl话题: cout话题: 诡异
进入Programming版参与讨论
1 (共1页)
f***t
发帖数: 25
1
对于简单类型的const_cast,下面代码:
int const i=2; //现在i值const了
int* k = const_cast(&i); //用引用也行,结果是一样的
*k=3; //借被const cast 的k改i值;成功;
cout< cout<<*k< cout<<&i< cout< 以上是VC6的编译。
把简单类型换成类就不会有这种现象,这是不是VC6的一个bug?
t****t
发帖数: 6806
2
it's undefined to modify a const value, no matter what method you use.
in other words, if a value is declared as const, you can NOT change it. if
you change it, it's undefined (means: wierd things happen).

【在 f***t 的大作中提到】
: 对于简单类型的const_cast,下面代码:
: int const i=2; //现在i值const了
: int* k = const_cast(&i); //用引用也行,结果是一样的
: *k=3; //借被const cast 的k改i值;成功;
: cout<: cout<<*k<: cout<<&i<: cout<: 以上是VC6的编译。
: 把简单类型换成类就不会有这种现象,这是不是VC6的一个bug?

f***t
发帖数: 25
3
Thanks,这个解释说的通.
不过,当时c++设计这种cast的动机,不就是为了改const的值吗?

【在 t****t 的大作中提到】
: it's undefined to modify a const value, no matter what method you use.
: in other words, if a value is declared as const, you can NOT change it. if
: you change it, it's undefined (means: wierd things happen).

l*********s
发帖数: 5409
4
Not really ; it is explained in Effective C++

【在 f***t 的大作中提到】
: Thanks,这个解释说的通.
: 不过,当时c++设计这种cast的动机,不就是为了改const的值吗?

t****t
发帖数: 6806
5
no, it's designed to change value referenced by const pointer/reference, but
itself not const. const value is never meant to be changed.

【在 f***t 的大作中提到】
: Thanks,这个解释说的通.
: 不过,当时c++设计这种cast的动机,不就是为了改const的值吗?

A**u
发帖数: 2458
6

你的c++功力真是令我太崇拜啦

but

【在 t****t 的大作中提到】
: no, it's designed to change value referenced by const pointer/reference, but
: itself not const. const value is never meant to be changed.

f***t
发帖数: 25
7
i c. 多谢大牛!

but

【在 t****t 的大作中提到】
: no, it's designed to change value referenced by const pointer/reference, but
: itself not const. const value is never meant to be changed.

t*******c
发帖数: 288
8
check out this:
http://en.cppreference.com/w/cpp/language/const_cast
The reference code has the same error as yours. As stated in Note, "Even
though const_cast may remove constness from any pointer or reference, using
the resulting pointer or reference to write to an object that was declared
const invokes undefined behavior."
1 (共1页)
进入Programming版参与讨论
相关主题
g++ default optimization errorquestion for C++ constant
c++标准函数传递一问C++菜问: 怎么这样也可以?
发个初级面试题post increment
C++ class cross reference problem有关objec access path的问题
C++ 的 问题question about const reference
conversion between const to nonconstc++环境入门问题
关于const_cast,地址一样,值不同?这个地址咋回事?
问题: C++ static_cast between int and float面试问题
相关话题的讨论汇总
话题: const话题: cast话题: endl话题: cout话题: 诡异