Constant member functions in c++ Relevancy | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

25th Apr 2017, 1:50 PM
Alex Lesage
Alex Lesage - avatar
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.
25th Apr 2017, 2:46 PM
Babak
Babak - avatar