看到一段关于 const 的讨论
摘录的一段:
My experience from other languages (C, C++, Objective C, Java, Smalltalk, etc) suggests that trying to add some notion of const to the language is detrimental. It's rarely of use to the compiler, adds little or no safety barrier for programmers, is confusing, and isn't always helpful documentation. I've seen grown men weep over C's const, and highly paid C++ professionals argue for hours over the meaning of const in C++.
The right place to annotate things with notions of constantness is the type hierarchy. See, for example, java.awt.Raster / java.awt.WritableRaster (from Java's 2D api), NSDictionary / NSMutableDictionary (from Objective-C's foundation classes).
drj
以为然也
Comments
shit,没看懂, 加强英语ing
Posted by: 天天基金网 | (7) February 11, 2008 05:40 AM
const bar foo() const
也只是让语义清晰嘛...
很多时候,C++不是把简单问题复杂化的语言,而是能够把复杂问题简单化的工具哦
Posted by: cnzhangzhen | (6) June 16, 2006 03:12 PM
另外在我看来, C++本身就是把简单问题复杂化的一个语系.
类似
const bar foo() const;
的语句的确很容易让人confusing...
Posted by: Zenberg | (5) June 16, 2006 01:40 PM
我觉得这话说的有失偏颇,const是个标识符,编译器是否去处理是编译器的事情,如果没有进行处理是编译器的问题。跟const本省没有关系。
至少在我的开发中(delphi, java)
const/final是经常使用的,可以使得程序自身的语义更加清晰。
Posted by: Zenberg | (4) June 16, 2006 01:34 PM
me too!
Posted by: nothanks | (3) June 16, 2006 10:10 AM
shit,没看懂, 加强英语ing
Posted by: xLight | (2) June 15, 2006 02:36 PM
"use const as much as you can..."
没什么感觉,至少可以帮助在运行之前能够抓住多一点的error.
最大的噩梦是编译通过,死活找不到运行出错的原因。而且面对的是一个巨大的工程时。
Posted by: cnzhangzhen | (1) June 15, 2006 09:22 AM