It seems that I should be writing here:
Should I declare any method that can be const with the const method?
No, the decision must be made at a different level during design. You should mark as const all methods that semantically do not modify the object. This may include some methods that really change some internal details that are not part of the perceived state of the object (and these attributes must be mutable ), and it may not include some methods that don't change anything at all.
enum impl { // different implementations of the algorithm one_implementation, another_implementation }; class example { mutable std::pair<bool, int> cache; protected: int a, b; public: example( int a, int b ) : cache(), a(a), b(b) {} virtual ~example() {} void set( int _a, int _b ) { cache.first = false; // invalidate previous result a = _a; b= _b; } int expensive_calculation() const { if ( !cache.first ) { cache.second = calculate(); cache.first = true; } return cache.second; } virtual void change_impl( impl x ) {} private: virtual int calculate() const = 0; };
In the current form, you cannot change the implementation, and change_impl not a constant, even if it does not modify any member attribute, it is not marked as const because it changes semantically.
On the other hand, the expensive_calculation() method does not semantically change the state of the object, the perceived state will be the same before and after the operation is called, but it modifies the cache attribute to speed up later calls (if the state has not changed). Thus, the method is const , and the cache is mutable .
source share