c++ - is it good practice to add const at end of member functions - where appropriate? -


is practice, in c++, add const @ end of member function definition every time function not modify object, i.e., every time function 'eligible' const? know it's necessary in case:

class myclass {  public: int getdata() const; };  void function(const myclass &m) { int = m.getdata(); dosomething... } 

but other this, , other uses of const actual functionality, adding const end change way code executed (faster/slower) or 'flag' compiler handle cases such 1 above? in other words, if const (at end) not needed functionality in class, adding make difference?

please see excellent article const correctness herb sutter (c++ standards committee secretary 10 years.)

regarding optimizations, later wrote this article states "const humans, rather compilers , optimizers." optimizations impossible because "too many things go wrong...[your function] might perform const_casts."

however, const correctness idea 2 reasons: cheap (in terms of time) assertion can find bugs; and, signals intention function should theoretically not modify object's external state, makes code easier understand.


Comments