问题描述
当我读到const_cast时,我很好奇,想知道如果我可以通过一个指针来修改一个常量变量,那就是
" const_cast"编辑。由于指针指向常量
变量,如果我通过指针更改了值,则应该修改常量
变量。嗯,这就是我的想法,直到
我写了以下内容并运行它。
#include< iostream>
使用std :: cout;
使用std :: endl;
int main()
{
const int i = 0;
int& j = const_cast< int&>(i);
j = 10; //我以为这会改变我,但它并没有
cout<< & i<< ENDL; //& i和& j与我预期的相同
cout<< & j<< endl;
cout<< i<< ENDL; //但我还是0
cout<< j<< ENDL; //和j是10!
int * p = const_cast< int *>(& i);
* p = 30; //我通过p改变我
cout<< & i<< ENDL; //我确认p指向我!
cout<< p
cout<< i<< ENDL; //我还是0!
cout<< * p<< ENDL; //怎么能* p那么30?
返回0;
}
我非常喜欢困惑。 & i和& j是相同的,这意味着它们指的是
相同的位置。 p也指向相同的位置。但是那么
我仍然可以是0而j是10而且我仍然是0和* p 30 ????
j分配了新内存空间?是否指向新位置?
有谁可以帮我弄清楚发生了什么事吗?
顺便说一句,我就是这样观察是标准行为或特定于我的
编译器(g ++版本3.2.3)?
非常感谢您提前!
While I was reading about const_cast, I got curious and wanted to know
if I could modify a constant variable through a pointer which has been
"const_cast"ed. Since the pointer would be pointing to the constant
variable and if I changed the value through the pointer, the constant
variable should have been modified. Well, that''s what I thought until
I wrote the following and run it.
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const int i = 0;
int &j = const_cast<int&>(i);
j = 10; // I thought this would change i, but it didn''t
cout << &i << endl; // &i and &j are same as I expected
cout << &j << endl;
cout << i << endl; // but i is still 0
cout << j << endl; // and j is 10!
int * p = const_cast<int *>(&i);
*p = 30; // I change i via p
cout << &i << endl; // I confirm that p is pointing to i!
cout << p << endl; // I confirm that p is pointing to i
cout << i << endl; // i is still 0!
cout << *p << endl; // how can *p be 30 then?
return 0;
}
I am very much confused. &i and &j are same, meaning they refer to the
same location. p is also pointing to the same location. But then how
can i still be 0 and j be 10 and i still be 0 and *p 30????
Is j allocated new memory space? Is p pointing to a new location?
Can anyone kindly help me figure out what is going on?
BTW, is what I am observing is standard behaviour or particular to my
compiler (g++ version 3.2.3)?
Thank you very much in advance!
推荐答案
是的。和不。没有标准行为。标准说
行为是_undefined_。
Yes. And no. There is no standard behaviour. The Standard says that
the behaviour is _undefined_.
对于cout<< i,编译器将0替换为cout语句,因为它原来是const。
For cout << i, the compiler has substituted 0 into the cout statement as it
was originally const.
声明一个const int对象不一定会分配任何内存
为它。就这样我们很清楚。编译器可以自由地使用
在声明时给出的const的实际值,而不是内存中的对象(顺便提一下,可能存在或不存在)。
Declaring a const int object does not necessarily allocate any memory
for it. Just so we''re clear on that. The compiler is free to use
the actual value of the const given to it at the moment of declaration
instead of the object in memory (which, incidentally, may or may not
exist).
是的,但是如果你取变量的地址并使用这个地址,那么
编译器必须为它分配内存。 />
True, but if you take the address of the variable and use this address, the
compiler must allocate memory for it.
这篇关于我可以通过从const_cast获得的指针修改常量变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!