+ 5
Constant member functions in c++ Relevancy
What is the point of making a member function into a constant. Non const objects from the class can still access them so how would they be useful?
1 Answer
+ 6
It's all about applying the least privilege principle .
1. constant member functions can't modify data members and any attempt leads to compilation error.
2. constant member functions has restricted to access to other constant member functions and any attempt to calling non-constant ones leads to compilation error.
3. Invoking a non-constant member function on a constant object leads to compilation error.
4. Any attempt to declare a constructor or destructor as constant leads to compilation error.
5. Declaring variables and objects as constant when appropriate leads to improving performance.