Should I declare any method that may be const const?

Simple question. Should I declare any method that can be const a const method? This includes methods that do not return any member variables, or return const references to member variables. Is there any reason not to do this (other than for obvious reasons that the compiler will indicate anyway)?

+6
source share
5 answers

The non const method cannot be called via a pointer to a constant object. Therefore, if a method can be const without declaring it const, it will impose artificial restrictions on its use.

In addition, creating the const method is an important semantic detail to give the user a sense of the effect that one would expect from his call.

+13
source

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 .

+6
source

Yes. According to Effective C ++, "use const whenever possible."

+5
source

If the method does not change the logical state of the object, you should mark the methods as const. This is a great service for clients of your objects, as they can be used to the full, taking into account const links / pointers.

+2
source

There is one case where I would think twice about using const: a virtual function of a base class. It may be good that inherited classes cannot be modified by their overridden function, but some developers may disagree and really have to jump through hoops because of this.

+2
source

Source: https://habr.com/ru/post/886175/


All Articles