开发者

Constant Member Functions

After reading this, it is my understanding that declaring a method as const prevents it from accidentally modifying the class's member variables.

  • Are const methods commonly used开发者_开发技巧?
  • Should they be used for everything that shouldn't modify member variables?


Yes, const should always be used, when appropriate.

It lets your compiler check your application logic, statically asserting const-correctness for free!

Some people even say that const should be the default, and you should be forced to use mutable for what is non-constant.


I use const extensively to communicate design intent. If I intended that a method was a pure query, and not a modification function, I would both enforce that and communicate it with a 'const' in the signature.

I encourage you to look at Meyer's thoughts on the matter of side-effect-free functions, and the implications that has on enabling testing.


Just my testimony.

Some years ago, I was still against the use of const, just because of the constraints in design and writing longer function signatures... and so on...

But one of my project leaders always insisted, all the time, reminding me: "You should use const function, it avoids accidents and doing non-sense".

And one day I faced an impossible bug to find. Days after days after days... A nightmare. The design was too big for me too see so as I could grasp it in its whole. I searched in vain until I decided I was lost.

Then I spent two days redefining ALL the functions that should be const. I mean it, two days. (Recompilations were long as it was a 5 millions lines of code project).

And then: simply I found the bug... rather the Compiler found the bug for me: In a getter-like method, which should have given me the prefered size of a gui control, the code was actually computing the size, but it was also caching its size and updating its size... Thus modifying the object.

Now, sometimes I forget to put const. But if I notice it, I correct it.


Adding const to a member function allows it to be called on const references to an object, because it guarantees that instance variables won't be changed. Const references occur in various places throughout the STL, so it makes sense to mark member functions as const where the function doesn't intend to modify the state of an object.

Note: it is possible to mark certain instance variables as mutable so they can be changed even by const functions. This is useful to implement look-up caches, for example.


Declaring a method that shouldn't modify member variables:

  1. Assures that what you think is what is happening, i.e. that you're not accidentally modifying a variable somewhere.
  2. Declares to callers of the function that this method doesn't modify member variables, removing the need to read over the code or rely on documentation that says so.

So yes, use const wherever it makes sense. They're not as widely used as I'd like to see though, most likely because the majority of developers don't see the huge benefit.


If you forget to mark an accessor as const, the compiler will not allow the method to be called on const objects or references to const objects. So yes, marking accessors as const is important.


If you have a const reference or pointer (i.e. pointer to const) of a class object then you can ONLY call const member methods of the class.

So if someone "forgot" to make a "get" method const, you would not be able to call it with a const reference (There is a workaround with const_cast but we don't want to use that!).

So yes, if the class is not going to be modified by the method then it should be const.

Note: there are some occasions that you do want to modify a variable as an "implementation detail", eg lazy-loading or to lock a mutex. In such a case you can still make the method const but make that member variable "mutable".

If you are writing a virtual method, it should be const if no derived class will need it to be mutable.


You should use the const keyword whenever possible.

It prevent you from the mistakes in the code.

It increase the readability of the code very much. Everyone who is reading the header and seeing const keywords can immediately understand that a const method does not change the state of an object and can be used with no being afraid he will change the object for example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜