c++ - 使 C++ 变量成为 const

标签 c++ constants static-analysis

我喜欢“const”。我希望每个“应该是常量”的变量和方法都是“常量”。问题是变量或方法是否“应该是常量”取决于调用树中更下方的方法/变量。是否有某种工具或某种过程可用于静态检查代码体并执行“自下而上的常量化”?

最佳答案

我不知道你问题的答案,但我想反驳这种说法

whether a variable or method "ought to be const" depends on methods/variables further down in the call tree

其实const应该是逻辑层面的。 IE。如果逻辑上不应更改,则应将其标记为 const。如果稍后是,那么您将遇到编译器错误,并且需要重新考虑更改的事实或您的初始假设。

规则是:

if something is const, it should not be changed

而不是

if something is not changed de facto, then let's make it const

关于c++ - 使 C++ 变量成为 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737670/

相关文章:

Java-Eclipse : Externalize Strings?

c++ - 定义 const 数组类型的语法

c - 在 C 宏中,是否应该更喜欢 do { ... } while(0,0) 而不是 do { ... } while(0)?

用于查找竞争条件/死锁的 C#/.NET 分析工具

python - 帮助避免 python/django 错误的工具或技术

c++ - "CV_EXPORTS_W"是什么意思

c++ - 为什么指针指向零值时是空白的?

c++ - 缓存友好矩阵,用于访问相邻单元格

c++ - 我可以在 deleteLater 之后再次使用 QTcpSocket 进行另一个连接吗?

javascript - 如果我可以声明 JS const,为什么还要使用 AngularJs .constant() ?