我可以通过从const_cast获得的指针修改常量变量吗? [英] Can I modify a constant variable through a pointer obtained from const_cast?

查看:57
本文介绍了我可以通过从const_cast获得的指针修改常量变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到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 << ENDL; //我确认p指向i

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!

推荐答案

CoolPint写道:
CoolPint wrote:
当我读到const_cast时,我很好奇并想知道
我是否可以修改常量通过已经被指定为const_cast的指针变量。


让我马上说出来:你可以修改它,但它导致未定义的

行为。一旦我们建立了这个,所有其他的东西都是猜测,

充其量。

因为指针指向常量
变量并且我改变了值通过指针,应该修改常量
变量。嗯,这就是我的想法,直到我写下以下内容并运行它。

#include< iostream>
使用std :: cout;
使用std :: endl;
int main()
{
const int i = 0;


声明一个const int对象并不一定为它分配任何内存

。就这样我们很清楚。编译器可以自由地使用

在声明时给予它的const的实际值

而不是内存中的对象(顺便说一句,可能或可能不存在


int& j = const_cast< int&>(i);


到目前为止,非常好。

j = 10; //我以为这会改变我,但它没有


确实如此。或者它没有。或者它确实如此,但随后将其更改为不知情的

给你。任何事情都可能发生。没有任何保证。你打破了你对编译器的承诺:''我'是不变的。你试图改变

吧。编译器(及其生成的代码)没有义务做任何可预测或有意义的事情。

cout<< & i<< ENDL; //& i和& j与我预期的相同
cout<< & j<< endl;
cout<< i<< ENDL; //但我仍然是0


嗯,这就是猜测开始的地方。 ''我'并不是真的0仍然。

它已被更改。然而,_code_被创建为仅使用''0''

而不是'i'来表示上述语句。允许编译器执行

。它优化了内存访问,因为你承诺不会以任何方式改变内存



cout<< j<< ENDL; //和j是10!

int * p = const_cast< int *>(& i);
* p = 30; //我通过p
改变我

未定义行为的另一个原因,就在这里。如果之前所有的赌注都已经结束了,那么现在他们真的很绝对。

cout<< & i<< ENDL; //我确认p指向i!
cout<< p << ENDL; //我确认p指向的是 cout<< * p<< ENDL; //怎么可以* p那么30?

返回0;
}

我非常困惑。 & i和& j是相同的,意思是它们指的是相同的位置。 p也指向相同的位置。但那么我怎么仍然可以0和j为10而且我仍然是0和* p 30 ????

是否为j分配了新的内存空间? p指向新位置吗?

任何人都可以帮我弄清楚发生了什么事吗?


我希望我有。如果碰巧有问题,可以提出更多问题。

BTW,我观察到的是标准行为还是特定于我的
编译器(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.
Let me say this right away: you can modify it, but it leads to undefined
behaviour. Once we established that, all other things are speculation,
at best.
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;
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).
int &j = const_cast<int&>(i);
So far, so good.
j = 10; // I thought this would change i, but it didn''t
It did. Or it didn''t. Or it did, but then changed it back unbeknownst
to you. Anything can happen. No guarantees whatsoever. You broke your
own promise to the compiler: ''i'' is constant. You attempted to change
it. The compiler (and the code produced by it) is not obligated to do
anything predictable or meaningful, for that matter.
cout << &i << endl; // &i and &j are same as I expected
cout << &j << endl;
cout << i << endl; // but i is still 0
Well, this is where the speculation begins. ''i'' is not really 0 "still".
It has been changed. The _code_ however was created to simply use ''0''
instead of ''i'' for the statement above. The compiler is allowed to do
that. It optimises away memory access since you promised that the memory
is not going to be altered in any way.
cout << j << endl; // and j is 10!

int * p = const_cast<int *>(&i);
*p = 30; // I change i via p
Another cause for undefined behaviour, right here. If all bets were off
before, now they are really REALLY off.
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?
I hope I have. Ask more questions if you happen to have them.

BTW, is what I am observing is standard behaviour or particular to my
compiler (g++ version 3.2.3)?




是的。和不。没有标准行为。标准说

行为是_undefined_。



Yes. And no. There is no standard behaviour. The Standard says that
the behaviour is _undefined_.


" CoolPint" <共****** @ yahoo.co.uk>在消息中写道
"CoolPint" <co******@yahoo.co.uk> wrote in message
当我读到const_cast时,我很好奇并想知道
我是否可以通过指针修改一个常量变量
"&的const_cast QUOT;编由于指针指向常量
变量,如果我通过指针改变了值,则应该修改常量
变量。嗯,这就是我的想法,直到我写下以下内容并运行它。


const_cast仅在原始对象为const时才能工作。

如果原始对象是const,编译器可以将其值替换为

cout语句等等,这样即使你通过指针修改变量

,cout语句也会打印原始值。在

更复杂的例子中,这个替换和评估const变量在

编译时间(我认为它被称为const折叠)导致了很好的

优化。编译器还可以将变量存储在只读内存中作为编译程序的一部分,因此如果通过指针修改变量,程序将崩溃。在其他情况下,const_cast可以作为你想要的


{
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!
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.
const_cast is only garaunteed to work when the original object was const.
If the original object is const, the compiler can substitute its values into
cout statements and so forth, so that even if you modify the variable
through a pointer the cout statement will print the original values. In
more complex examples, this replacing and evaluating of const variables at
compile time (I think it''s called const folding) leads to great
optimizations. The compiler can also store the variable in read-only memory
as part of the compiled program, so that if you modify the variable through
a pointer the program will crash. In other cases, const_cast may work as
you want.
{
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!




对于cout<< i,编译器将0替换为cout语句,因为它原来是const。



For cout << i, the compiler has substituted 0 into the cout statement as it
was originally const.


" Victor Bazarov" <五******** @ comAcast.net>在消息新闻中写道:O5RLc.47
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:O5RLc.47
#include< iostream>
使用std :: cout;
使用std :: endl;
int main()
{
const int i = 0;
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const int i = 0;



声明一个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.

int& j = const_cast< int&>(i);
cout<< & j<< endl;
int &j = const_cast<int&>(i); cout << &j << endl;



这篇关于我可以通过从const_cast获得的指针修改常量变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆