Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.
When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.
void f(Foo &foo, const Bar &bar) { ... }
In this case the caller will know that the foo might be modified, but the bar will not. The compiler will enforce this when compiling the body of f(), so that bar is never modified and never passed on to another function that might modify it.
All of the above safeguards can be bypassed using const_cast, which is why such a cast is considered "dangerous" (or at the very least, suspicious).
I'm not so convinced on the "safety" aspect of const (there's pros and cons)....however, what it does allow is certain syntax that you can't do without const
void blah(std::string& x)
{}
can only take a std::string object... However if you declare it const :-
void blah(const std::string& x) {}
you can now do
blah("hello");
which will call the appropriate constructor to make a std::string
另外,通过避免使用 #define 来定义常量并使用 const,可以提高代码的类型安全性。 C++ 编程实践指南 - 第 2.1 项
Also, by using const, you state your intention for the use of a variable or parameter. It is a good programming practice ("C/C++ Coding Style & Standards", item 2.3).
Also by avoiding using #define's to define constants and using const's, you increase the type-safety of your code. C++ Programming Practice Guidelines - item 2.1
The contract aspect of const is also important to the compiler's optimizer. With certain const declarations, optimizations involving loop invariants are easier for the optimizer to spot. This is why const_cast is truly dangerous.
发布评论
评论(6)
Const 对于传递给函数的指针或引用特别有用——它是一种可以立即理解的“API 契约”,即函数不会更改传递的对象。
另请参阅:http://www.parashift.com /c++-faq-lite/const- Correctness.html#faq-18.4
Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.
See also: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4
当在函数中用作常量引用时,它让调用者知道传入的内容不会被修改。
在这种情况下,调用者将知道
foo
可能会被修改,但bar
不会。 编译器将在编译f()
主体时强制执行此操作,以便bar
永远不会被修改,也永远不会传递给可能修改它的另一个函数。所有上述保护措施都可以使用 const_cast 来绕过,这就是为什么这样的强制转换被认为是“危险的”(或者至少是可疑的)。
When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.
In this case the caller will know that the
foo
might be modified, but thebar
will not. The compiler will enforce this when compiling the body off()
, so thatbar
is never modified and never passed on to another function that might modify it.All of the above safeguards can be bypassed using
const_cast
, which is why such a cast is considered "dangerous" (or at the very least, suspicious).看起来很明显 - 它可以防止您修改不应该修改的内容。
编辑:如需更多指导,请参阅 Herb Sutter。
Seems pretty obvious - it keeps you from modifying things that shouldn't be modified.
Edit: for more guidance, always look to Herb Sutter.
我不太相信 const 的“安全”方面(有优点和缺点)......但是,它允许的是某些语法,没有 const 就无法做到
只能采用 std::string 对象...但是,如果您将其声明为 const :-
您现在可以
调用适当的构造函数来创建 std::string
I'm not so convinced on the "safety" aspect of const (there's pros and cons)....however, what it does allow is certain syntax that you can't do without const
can only take a std::string object... However if you declare it const :-
you can now do
which will call the appropriate constructor to make a std::string
此外,通过使用 const,您可以声明使用变量或参数的意图。 这是一种很好的编程实践(“C/C++ 编码风格和标准” ,第 2.3 项)。
另外,通过避免使用 #define 来定义常量并使用 const,可以提高代码的类型安全性。
C++ 编程实践指南 - 第 2.1 项
Also, by using const, you state your intention for the use of a variable or parameter. It is a good programming practice ("C/C++ Coding Style & Standards", item 2.3).
Also by avoiding using #define's to define constants and using const's, you increase the type-safety of your code.
C++ Programming Practice Guidelines - item 2.1
const 的契约方面对于编译器的优化器也很重要。 通过某些 const 声明,优化器更容易发现涉及循环不变量的优化。 这就是为什么 const_cast 确实很危险。
The contract aspect of const is also important to the compiler's optimizer. With certain const declarations, optimizations involving loop invariants are easier for the optimizer to spot. This is why const_cast is truly dangerous.