如何改变C ++中的变量的常数? [英] How to change the constness of a variable in C++?

查看:138
本文介绍了如何改变C ++中的变量的常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码

const int i = 10;
cout<<i<<"\n";
int *ip = (int*)&i;
*ip = 20;
cout<<i<<"\n";

我期望输出为 10 code> 20 。

但我得到的输出为 10 10

I was expecting output as 10 and 20.
But I am getting output as 10 and 10.

我能够编译程序,我在编译时或运行时没有得到任何错误,但没有得到预期的结果。如果我已经删除了变量的const为什么新的值没有分配给它?

I am able to compile the program and I don't get any error at compile time or runtime but not getting the expected outcome. If I have removed the constness of the variable i why the new value is not assigned to it?

如果有人可以解释发生了什么以及如何实现我的预期结果,我将非常感激。

I would be grateful If somebody could explain whats happening and what can be done to achieve my expected result.

推荐答案


如何更改C ++中变量的常数?

你永远不应该改变一个固有的 const 限定变量的常数。

An const 限定的变量被声明,目的是在初始化之后不应该修改它。通过尝试改变const打破与编译器的合同。

You should never change the constness of an inherently const qualified variable!
An const qualified variable is declared with the aim that it should not be modified after it is initialized. By attempting to change the constness you break the contract with the compiler.

如果你需要修改一个变量,只要不将它声明为 const

If you need to modify a variable, simply do not declare it as const.

请注意,C ++提供 const_cast 来删除或添加const变量。但是,在删除常量的时候,应该使用const来删除指向不是原始常量的引用/指针的常量。

Note that C++ provides const_cast to remove or add constness to a variable. But, while removing constness it should be used to remove constness off a reference/pointer to something that was not originally constant.


发生了什么?

正在发生的是未定义行为

您不应该更改 const 限定变量的常数,如果这样做通过一些指针hackery什么你得到的是未定义的行为。

You should not be changing the constness of an const qualified variable, If you do so through some pointer hackery what you get is Undefined behavior.

在未定义行为的情况下,标准不要求编译器提供编译时诊断。请注意,如果以最高警告级别编译,某些编译器可能会警告您。

In case of undefined behavior the standard does not mandate compilers to provide a compile time diagnostic.Note though that some compilers may warn you if you compile with highest warning level.

此外,未定义的行为意味着当您运行程序时,任何事情都可能发生,编译器可能会显示任何结果,并且不保证解释。

Also, undefined behavior means that when you run the program anything can happen and the compiler might show you any result and it does not warrant an explanation.


为什么会产生结果?

在这种情况下,由于 i 是常量,可能编译器将const变量作为其优化的一部分,因此指针hackery不会影响 i 的值,因为它已经被编译器内联,它只是将inline值放在 i 代码。

In this case since i is constant probably the compiler inlines the const variable as a part of its optimization and hence the pointer hackery does not affect the value of i since it already inlined by compiler it simply puts the inlined value wherever i is encountered in the code.

请注意,编译器会这样做,因为您与编译器签订了合同,

Note that the compiler does so because you made an contract with the compiler,

Heres my i and it will never be changed throughout the lifetime of my program.

编译器可以随意应用任何可能需要的优化,只要它遵守合同,并且在这种情况下这样做。

The compiler is free to apply whatever optimizations it can want as long as it adheres to the contract and it does so in this case.

这篇关于如何改变C ++中的变量的常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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