O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

const Member Functions

If you declare a class member function to be const, you are promising that the method won't change the value of any of the members of the class. To declare a class method as constant, put the keyword const after the parentheses, but before the semicolon. The declaration of the constant member function SomeFunction() takes no arguments and returns void. It looks like this:

void SomeFunction() const;

Get accessor functions (some programmers call them “getter functions”) are often declared as constant functions by using the const modifier. The Cat class has two accessor functions:

 void SetAge(int anAge); int GetAge();  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required