22.5. The const function declaration

When you look at documentation on classes, you see a lot of pesky const. What’s all that about? Well, first off let’s say that if they confuse you too much, it would in fact be OK to just leave them all out. But there is a reason for them. It’s generally considered a good idea to put const after the declaration of any function with does not change the values of a class’s private fields.

int Func(SomeClass input)const; //Doesn’t change the caller class 
int Func(SomeClass input); //May change the caller class 

Meticulous programmers (and that’s what we should all want to be!) use const as a way of telling the compiler to warn you if it finds anything in that function’s code which changes a private class member. ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.