What is const function and const object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

What is const function and const object?

5th Apr 2017, 5:34 PM
dhiraj jadhav
dhiraj jadhav - avatar
2 Answers
+ 3
A const object can't have any of its member variables modified. It's as if you've applied const to all of its member variables. A const function is a promise to the compiler that the function will not change the state of the object (or, in other words, it won't change any of the member variables). When you have a const object, you can only call its const functions. Trying to call any other member functions will give you a compiler error. The point of using const objects is so you can pass them into functions by reference while assuring the user that the object passed in will not be modified. This also keeps you from accidentally modifying it where you shouldn't be.
5th Apr 2017, 9:14 PM
Squidy - avatar
As I know variables can get different values but constants (like pi) never can get a different value
5th Apr 2017, 6:48 PM
Bülent Gulistan Gözen
Bülent Gulistan Gözen - avatar