views:

360

answers:

7

Do you find it helpful?

+24  A: 

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.

Mykola Golubyev
And if the function isn't even touching the object state but purely transforms input args into the return value, then it should be static. Static > const member > normal member.
Lucas
+6  A: 

When you have a const object, the only methods that the compiler will let you call are those marked safe by the const keyword. In fact, only member methods make sense as const methods.

In C++, every method of an object receives an implicit this pointer to the object; A const method will simply receive a const this pointer.

Daniel Vassallo
+3  A: 

Assuming you're talking about methods, as in:

struct Example {
  void f() const;
};

Then if they should be callable on a const object, the method should be const.

Roger Pate
+11  A: 

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.

jasonline
+2  A: 

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.

Arve
The alternative is to wrap the API and determine if it really SHOULD take cost data and if so cast away the constness of the input data inside your wrapper. This means that you only need the ugly casting ONCE and you can comment the fact that you determined that this was safe and correct via your testing, etc. IMHO most if not all 'old APIs' should be wrapped...
Len Holgate
A: 

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.

Martin
Functions which don't change the "logical value" can still be const, you just have to use mutable data members for your cache.
Roger Pate
Yes, agree with Roger's comment. Data members declared as mutable can be changed inside a const function.
jasonline
+1  A: 

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* and const 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.

timday