Do you need to define const functions when you want to create a new const instance of a class?
Somehow my compiler dont find the "regular" (not const) functions, when i try to access them from a const class instance.
Do you need to define const functions when you want to create a new const instance of a class?
Somehow my compiler dont find the "regular" (not const) functions, when i try to access them from a const class instance.
Yes, if an object is
const, you can only callconstfunctions on that object.If a function is not marked
const, the compiler must assume that it is allowed to change the members of the class. And since you can't change the members of aconstclass instance, you cannot call non-constfunctions on that instance either.Note that the opposite case is different - there is no problem calling
constfunctions on non-constobjects. Therefore it is recommended to always mark member functions asconstwherever it makes sense to do so. It is important to mark function parameters, reference variables, etc. asconstfor many of these same reasons. The community typically refers to this practice as maintaining "const correctness."