Simple question. Should I declare any method that can be const a const method? This includes methods that don’t return any member variables, or return const references to member variables. Is there any reason not to do so (apart from the obvious reasons, which the compiler will point out anyway)?
Simple question. Should I declare any method that can be const a const method?
Share
A non-
constmethod cannot be called through a pointer to a constant object. So if method can beconst, not declaring it const would impose artificial limits on its use.Apart from that, making a method
constis an important semantic detail to give the user a feeling of the effect that could be expected from calling it.