关于const修改 [英] regarding const modification
本文介绍了关于const修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
#include" iostream.h"
void main( void)
{
const int i = 10;
int * p;
p = const_cast< int * >(& i);
* p = 111;
cout<< i<<"" "<< * p;
}
我在VC ++ 6上编译,输出为:10 111
>
我没有得到..为什么我和* p的价值不同?
任何人都可以帮助我吗?
谢谢
Vijay
Hi ,
Code:
#include "iostream.h"
void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}
I am compiling on VC++ 6, and output is : 10 111
I am not getting.. why value of i and *p is different??
Can any one help me?
Thanks
Vijay
推荐答案
1)示例应该是正确的,以便编译和链接
2)无论如何:看看代码被反汇编 - 编译器在堆栈上推送
10替换我。
Vijay写道:
1) example should be correct enough to compile and link
2) anyway: see the code disassembled - compiler replaces i by pushing
10 on stack.
Vijay wrote:
代码:
#include" iostream.h"
void main(void)
{
const int i = 10;
int * p;
p = const_cast< int *>(& i);
* p = 111;
cout<< i<<<" << * p;
}
我正在编译VC ++ 6,输出是:10 111
我没有得到...为什么i和* p的值是不同的??
任何人都可以帮助我吗?
Vijay
Hi ,
Code:
#include "iostream.h"
void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}
I am compiling on VC++ 6, and output is : 10 111
I am not getting.. why value of i and *p is different??
Can any one help me?
Thanks
Vijay
Vijay写道:
Vijay wrote:
代码:
#include" iostream.h"
void main(void)
{
const int i = 10;
int * p;
p = const_cast< int *>(& i);
* p = 111;
cout<< i<<<" << * p;
}
我正在编译VC ++ 6,输出是:10 111
我没有得到...为什么i和* p的值是不同的??
任何人都可以帮助我吗?
Hi ,
Code:
#include "iostream.h"
void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}
I am compiling on VC++ 6, and output is : 10 111
I am not getting.. why value of i and *p is different??
Can any one help me?
你的错误就是对这段代码抱有期望/>
未定义的行为。
最好
Kai-Uwe Bux
Your mistake is to have expectations about this code, which exhibits
undefined behavior.
Best
Kai-Uwe Bux
" Vijay" < MT ****** @ gmail.com>在消息中写道
|
|
|代码:
| #include" iostream.h"
非标准标题。阅读无扩展标题。
| void main(void)
未定义的行为。它是int main()
| {
| const int i = 10;
| int * p;
| p = const_cast< int *>(& i);
未定义的行为。你不能抛弃一个对象的常量,这个对象原本是const。
| * p = 111;
| COUT<< I<<" "<< * p;
| }
Sharad
"Vijay" <mt******@gmail.com> wrote in message
| Hi ,
|
| Code:
| #include "iostream.h"
Non standard header. Read about extensionless headers.
| void main(void)
Undefined behavior. It''s int main()
| {
| const int i=10;
| int *p;
| p=const_cast<int*>(&i);
Undefined behavior. You can''t cast away the constness of an object that was
originally const.
| *p=111;
| cout<<i<<" "<<*p;
| }
Sharad
这篇关于关于const修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文