如果您已经知道变量应该是常量,为什么还要使用关键字 const? [英] Why would you use the keyword const if you already know variable should be constant?

查看:10
本文介绍了如果您已经知道变量应该是常量,为什么还要使用关键字 const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当变量的值不应该被修改时,我正在阅读的许多书籍都使用关键字 const.除了向代码的读者说明如果您修改此变量可能会导致错误(您可以使用注释来执行此操作),为什么您需要该关键字成为任何编程语言的一部分?在我看来,如果您不想修改变量,那就不要.

Many of the books that I am reading use keyword const when the value of a variable should not be modified. Apart from specifying to readers of the code that you may cause errors if you modify this variable (you can use comments to do this), why would you need that keyword to be a part of any programming language? It seems to me that if you don't want a variable modified, simply don't.

有人可以为我澄清一下吗?

Could someone clarify this for me?

推荐答案

除了向代码的读者说明修改此变量可能会导致错误(您可以使用注释来执行此操作)

Apart from specifying to readers of the code that you may cause errors if you modify this variable(you can use comments to do this)

不是可能";导致你的程序出错.

Not "may"; will cause errors in your program.

  • C++ 编译器将通过编译失败和诊断消息(编译器错误")强制执行,无需注释;
  • AC 编译器大部分都会强制执行它,尽管它的标准库由于遗留问题而存在漏洞,例如 strchr,并且它有一些相当宽松的隐式转换规则这可以让你在没有意识到的情况下放弃 constness.但是,编译成功并不意味着没有错误.不幸的是,它确实意味着错误可能是程序中的细微错误,也可能是严重的严重崩溃.
  • A C++ compiler will enforce it with compilation failures and diagnostic messages ("compiler errors"), with no need for comments;
  • A C compiler will enforce it for the most part, though its standard library has holes thanks to legacy, such as strchr, and it has some rather lenient implicit conversion rules that can allow you to drop constness without realising it quite easily. However, just because you got a successful compilation doesn't mean that you don't have errors; unfortunately, it does mean that the errors can be subtle bugs in your program, as well as big, spectacular crashes.

无论哪种方式,您的程序都保证在其中包含一个错误.

Either way, your program is guaranteed to contain an error inside it.

在我看来,如果您不想修改变量,就不要修改.

It seems to me that if you don't want a variable modified, simply don't.

这一切都很好,但没有人是完美的.程序员会犯错误.这允许编译器 —它从不犯错误(至少,通常不会)—向您指出它们.

Well that's all well and good, but nobody's perfect. Programmers make mistakes. This allows the compiler — which never makes mistakes (at least, not usually) — to point them out to you.

当您在远离创建位置的地方使用很多很多行代码时,它特别有用.它越远,修改它就越容易,而不会意识到你不应该这样做.对于大型、复杂的代码库,这是必须的.

It's of particular use when you're using some data variable many, many lines of code away from where it was created. The further away it is, the easier it is to modify it without realising that you were not supposed to. For large, complex code bases it is simply a must.

您会在代码库中获得一个新的衡量标准,即可证明性正确性稳定性,以及大量可能导致非常微妙和讨厌的错误.当您的编译器知道某些值在编译后不会改变时,也有大量的优化机会(在某些情况下).

You get a new measure of provability, correctness and stability in your code base, as well as a huge chunk off possible causes of really subtle and nasty bugs. There are also vast optimisation opportunities for your compiler (in some cases) when it knows that some value won't change after compilation.

我们可以整天列出优点,但实际上,在您使用过这样的代码库之前,您不会完全了解它.

We could list the advantages all day but, really, you won't fully grok it until you've worked on such a codebase.

事实上,在一个完美的世界里,所有变量默认都是const,你需要用关键字mutable来声明它们 以便能够更改它们.C++ 倒退了.

In fact, in a perfect world, all variables would be const by default, and you would need to declare them with the keyword mutable to be able to change them. C++ is backwards.

这篇关于如果您已经知道变量应该是常量,为什么还要使用关键字 const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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