Ive searched through the archived posts but I couldnt find anything similar.
Just a simple question: whats the best practice in declaring inspecting functions like:
int foo(int a) const { return a+1; }
Im referring to the const keyword, do you usually declare it at the end of an inspecting function? Im just asking because I usually do, even if 99% I wont declare any const class.. I just keep on tellin myself that it could save me some time if I ever should need to, but I was wondering if anybody else really cares about declaring const functions or if Im just paranoid about typesafe code
Assuming that you are talking about member functions (non member functions cannot be
constever), you should write const-correct code always, and that means that each and every function that does not modify the visible state of the object should beconst.Even if you don’t ever create a constant object of the type, in many cases you will pass the object to a function through const references and in that case, only
constmember functions can be called on the object (Incidentally this means that you should pass the object by constant reference to any function that does not need to change the state of the received object)