Const Correctness Part 5

by Darren Collins
Sunday, 4 April 2004

Now that everyone understands the meaning of the keyword const in variable declarations and function parameter lists (you do understand, don't you? Go back and read the last 4 articles if you don't!), I'd like to look at the use of const to create constant member functions.

Sometimes, you create a member function for your class that modifies the 'this' object. Other times, the member function just returns a value or performs some other operation that doesn't change the 'this' object. Marshall Cline and Greg A. Lomow, in the C++ FAQs Book, call these two types of member functions 'mutators' and 'inspectors' respectively.

A common example of mutators and inspectors are get and set functions for member variables. Have a look at this simple example:

class Competitor
{
  public:
    Competitor(int initage = 0)
      : _age(initage) {}
    void setAge(int newage) { _age = newage; }
    int getAge() { return _age; }
  private:
    int _age;
};

The setAge() member function is a mutator, as it changes the calling Competitor's _age member variable. The getAge() member function is an inspector, as it doesn't modify the state of the calling Competitor object.

You can document this in the source code by using the const keyword after the getAge() function declaration:

class Competitor
{
  public:
    Competitor(int initage = 0)
      : _age(initage) {}
    void setAge(int newage) { _age = newage; }
    int getAge() const { return _age; }
  private:
    int _age;
};

This tells other programmers (and the compiler!) that you're not going to modify the calling object. If someone later modifies your class, and tries to make it change a member variable or call a non-const member function, the compiler will catch the error.

Another (usually less significant) benefit is that the const keyword, by guaranteeing that the calling object won't be modified, allows the compiler to apply optimisations to your function call in some situations. This isn't usually the sole motivation for making a member function const, and obviously varies between compilers.

A non-const object can invoke both non-const and const member functions. A const object, however, can only invoke const member functions. This is shown below:

Competitor A(10);       // non-const object
Competitor const B(9);  // const object

int i = B.getAge();     // OK - const member
A.setAge(i);            // OK
B.setAge(15);           // Error - can't modify const B

Summary

  1. Declare member functions that don't modify the object's state as const, to guard against possible future mistakes.
  2. Any object (const or non-const) can invoke const member functions.
  3. Const objects can't invoke non-const member functions.

 


Related Articles
Index
- Links - C/C++
- Code Layout Styles
- Const Correctness Part 1
- Const Correctness Part 2
- Const Correctness Part 3
- Const Correctness Part 4
- Const Correctness Part 5
- Const Correctness Part 6

This site Copyright 1999-2005 Darren Collins.