DEV Community

Jumana Bohra
Jumana Bohra

Posted on • Edited on

Something I learnt more about const keyword

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

Top comments (0)