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

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

问题描述

很多的,我看用关键字常量当一个变量的值不应修改书。除了指定到code,如果你修改这个变量,你可能会导致错误的读者(你可以使用注释来做到这一点),你为什么会需要一个关键字是任何编程语言的一部分吗?在我看来,如果你不想要的变量修改呢,根本就没有。

有人能澄清这一点给我吗?


解决方案

  

除了指定到code,如果你修改这个变量,你可能会导致错误的读者(你可以使用注释来做到这一点)


不是可能;的将会的原因的错误在你的程序。


  • C ++编译器将编译失败和诊断信息(编译器错误)强制执行,而无需征求意见;

  • C编译器将强制执行它的在大多数情况下的,但其标准库有感谢遗留孔,如和strchr ,它有一些比较宽松的隐式转换规则,可以让你放弃常量内斯不很容易地意识到这一点。然而,仅仅因为你有一个成功的编译并不意味着你没有错误;不幸的是,它的确实的意思是错误可以在你的程序微妙的错误,以及大,壮观的崩溃。

无论哪种方式,你的程序是保证包含的错误里面


  

在我看来,如果你不想要的变量修改呢,根本就没有。


嗯,这一切都很好,但没有人是完美的。程序员犯错误。这使得编译—从未看走眼(至少不是通常)—他们指出你。

这是特别有用的,当你使用code从它被创造,其中客场的一些数据变量很多,很多行。进一步远离它,就越容易修改它没有意识到,你是不应该。对于大型,复杂的code碱基那简直是必须的。

您获得的可证 正确稳定性在code基,以及大块掀起了新措施真是微妙和讨厌的错误的可能原因。也有广阔的优化的机会您的编译器(在某些情况下),当它知道有些值不会编译后更改。

我们可以列出所有优点一天,但,真的,你不会完全的神交的它,直到你在这样一个codeBase的工作。

事实上,在一个完美的世界,所有变量将是常量默认,然后你就需要声明它们关键字可变来能够改变它们。 C ++是倒退。

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.

  • 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.

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天全站免登陆