您多久声明一次函数为 const?
您觉得有帮助吗?
Do you find it helpful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您觉得有帮助吗?
Do you find it helpful?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
每当您知道该方法不会更改对象的状态时,您都应该将其声明为常量。
它有助于阅读您的代码。当您尝试更改对象的状态时,它会有所帮助 - 编译器会阻止您。
Every time You know that method won't change state of the object you should declare it to be constant.
It helps reading your code. And it helps when you try to change state of the object - compiler will stop you.
尽可能多地进行。不需要更改数据成员的函数应声明为 const。这使得代码更容易理解,并且可以为编译器提供优化提示。
As often as possible. Functions that don't need to change data members should be declared as const. This makes code more understandable and may give hint to the compiler for optimization.
当您拥有
const
对象时,编译器允许您调用的唯一方法是那些由const
关键字标记为安全的方法。事实上,只有成员方法才有意义作为 const 方法。在 C++ 中,对象的每个方法都会接收一个指向该对象的隐式
this
指针;const
方法只会接收一个const
this
指针。When you have a
const
object, the only methods that the compiler will let you call are those marked safe by theconst
keyword. In fact, only member methods make sense asconst
methods.In C++, every method of an object receives an implicit
this
pointer to the object; Aconst
method will simply receive aconst
this
pointer.假设您正在谈论方法,如下所示:
那么如果它们应该在 const 对象上可调用,则该方法应该是 const。
Assuming you're talking about methods, as in:
Then if they should be callable on a const object, the method should be const.
不够频繁......
虽然所有答案都是正确的,但如果您使用的库不是 const 正确 那么很难在所有应该使用它的地方使用 const 。
如果您有一个旧的 API,它采用 char *,出于所有逻辑目的,该 API 应该是 const char *,那么您要么必须忘记代码中的 const,要么执行一些操作丑陋的铸造。在那种情况下我会忘记 const。
Not often enough....
While all the answers are correct, if you are using a libary that is not const correct then it is difficult to use const all the places you should use it.
If you have an old API that takes a char * that for all logical purposes should be a const char *, then you either have to forget const in your code or do some ugly casting. In that case I forget const.
我几乎在每一个机会都使用 const,并且喜欢它既提供意图文档又强制遵守该意图的事实。语言特性没有比这更好的了,但奇怪的是 const 却不受人喜爱。 (现实情况似乎是,大多数自称为 C++ 的程序员无法解释
int*、int*const、const int*
和const int*const
之间的区别>.)虽然由于它的“C”起源,它永远不会发生,但我经常认为,如果 const 是默认值,并且有必要大量使用(比如)“var”或一些类似的关键字,C++ 将是一种更好的语言允许变量的构建后修改。
I use const at almost every opportunity, and like the fact it provides both documentation of intent and enforces compliance with that intent. Language features don't get much better than that, and yet const is curiously unloved. (The reality seems to be that the majority of self-proclaimed C++ coders can't explain the difference between
int*, int*const, const int*
andconst int*const
.)While it could never have happened due to its 'C' origins, I often think C++ would be a better language if const had been the default and a liberal sprinkling of (say) 'var' or some similar keyword was necessary to allow post-construction modification of variables.
我曾经将函数声明为 const 但现在我很少再这样做了。
主要问题是,如果我想将函数从 const 更改为非常量,则意味着调用该函数的所有其他 const 函数也需要更改为非常量。
由于优化,这种情况发生的次数比我想象的要多。例如,我有一个 GetData() 函数,它用于返回指向数据的指针,但我后来优化为仅在 GetData() 最终被调用时才设置数据(这会更改对象的状态,因此它不再是 const功能)。
对于其他可以在不改变对象状态的情况下进行一些计算的函数来说也是如此,但在某些时候,缓存结果更有意义,因为该函数被多次调用并且是一个瓶颈。
同样在实践中,至少对于我的项目来说,我发现将函数声明为 const 几乎没有什么好处。
I used to declare functions as const but now I rarely if ever do it anymore.
The main problem was that if I wanted to change a function from const to non-const, it would mean that all other const functions calling that function would also need to be changed to non-const.
That happened more often than I thought due to optimization. For example I had a GetData() function which used to return a pointer to the data, but I later optimized to only set up the data if GetData() ends up being called (which changes the object's state, so it's no longer a const function).
Same thing for other functions that could do some calculation without changing the object's state, but at some point it made more sense caching the result since the function was called many times and was a bottleneck.
Also in practice, at least for my project, I saw very little benefit from declaring my functions as const.