Lecture Notes for CS 509, Advanced Programming II

25 March 2004, Classes and Conversions


A logically constant class instance is one that's externally constant but internally variable (a class instance that's also internally constant is physically constant).

Because logical constness is a useful idea, and because const-casting behavior is implementation dependent, C++ supports the mutable keyword to mark instance fields that are never constant, even if the containing instance is.

The mutable keyword provides a more reliable way to count method calls

class C {
  public:

  bool C::find(int value) const {
    find_count++;
    return fnd(value);
    }

  private;

    mutable unsigned find_count;
  };

Even though C::find() is marked const, find_count is marked mutable and the code compiles without further adjustments.


This page last modified on 6 April 2004.