Something I learnt more about const keyword

jumana profile image Jumana Bohra Updated on ・1 min read

const keyword has multiple usages in c++ as per the position of the keyword. While going through my project code const captured my attention again, being placed at the end of function declaration. Something like this:

ReturnType FunctionName (FormalParameters...) const;

While discovering its use case I found it quite helpful. This type of function is called a "const function". Such function restricts changing it's class variable inside it. It is a compilation error for const function to change it's member class variable.

Well the restriction can be loosened by the storage class specifier mutable. However it can be used efficiently for getter methods.

Compiler error


markdown guide