Again is about making sure clients use your types properly. You may realise that for built in types passing by reference to const is often more efficient than pass by value. This is to avoid the construction because of the object using in the function. Note that it needs to be const so you are sure that you object won't be modified.
class CMyClass{ //... void doSomething(); void doSomethingConst() const ;}void SomeFunction( const MyClass &obj ){ obj.doSomething(); //This won't compile, see below. obj.doSomethingConst();}
Notice that the call to doSomething won't compile. That’s because its not been declared const. The compiler can't be sure that you are invoking a function call that will not modify your object. doSomethingConst is fine, as you have declared it a const function. In other words you are telling the compiler that this function will not modify anything in the object (i.e. member variables ).
In general you should use const quite a lot. When you make new classes you are actually making new types. This means you need to make sure that people use your types properly, or even that you use them properly yourself. If a member
function does not modify any par of the object then it should be declared const, so it can be used by const objects of your type.