Const (computer programming)

This is an old revision of this page, as edited by TheIncredibleEdibleOompaLoompa (talk | contribs) at 00:06, 28 April 2005 (+mutable keyword. might deserve its own article, but for now its here). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Const correctness is a programming language feature, its most famous implementor being C++. In languages supporting this feature, variables can be said to be 'const'. When a const object is referenced, only the methods declared as const may be called. This allows programmers to formalise a specific design contract: they can promise at compile-time that a parameter of a function cannot be modified destructively.

Of course, const methods can only call other const methods, and cannot assign member variables.

In C++, a member variable declared with the mutable keyword can be modified when the method or even the entire class is const. This can be used to maintain internal data that needs to change even in const objects, such as in reference counting and caching.