Const (computer programming): Difference between revisions

Content deleted Content added
:{{merge|immutable object}}
Line 8:
 
==C/C++ Syntax==
In C++ all data types, including those defined by the user, can be declared <code>const</code>, and all objects should be unless they need to be modified. Such proactive use of <code>const</code> makes values "easier to understand, track, and reason about,"<sup>[[#Footnotes|1]]</sup> and thus, it increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates something about a value's intended use.
For simple types, the <code>const</code> can go on either side of the type (for historical reasons). For instance:
 
===Simple data types===
<code>
BothFor aresimple data types, applying the <code>const</code> qualifier is straightforward. It can go on either side of the type for historical reasons (that is, <code>const char foo = 'a';</code>s withis constantequivalent valueto <code>char const foo = 'a';</code>). On some implementations, using <code>const</code> on both sides of the type (for instance, <code>const char const</code>) generates a warning but not an error.
const char foo = 'a';
</code>
 
is equivalent to
 
<code>
char const foo = 'a';
</code>
 
Both are <code>char</code>s with constant value. On some implementations, using <code>const</code> on both sides of the type (for instance, <code>const char const</code>) generates a warning but not an error.
 
===Methods===
In C++, methods of a <code>struct</code> or <code>class</code> can be tagged as <code>const</code>, and while <code>const</code> methods can be called by <code>const</code> and non-<code>const</code> objects alike, non-<code>const</code> methods can only be invoked by non-<code>const</code> objects. This example illustrates:
 
<code>
class C
{
int i;
private:
int Get() const { return i; } // Note the const tag
void Set( const int j ) { i = j; }
};
 
void Foo( C& nonConstC, const C& constC )
{
int y = nonConstC.Get(); // Ok
int x = constC.Get(); // Ok: Get() is const
 
nonConstC.Set( 10 ); // Ok: nonConstC is modifiable
constC.Set( 10 ); // Error: Set() might modify constC!
}
</code>
 
===Pointers and references===
Line 53 ⟶ 22:
int const * const constPtrToConst )
{
int const i = 42;
*ptr = 0; // Ok: modifies the pointee
Line 80 ⟶ 49:
 
Even more complicated declarations can result when using multidimensional arrays and references (or pointers) to pointers. Generally speaking, these should be avoided or replaced with higher level structures because they are confusing and prone to error.
 
===Methods===
In C++,order methodsto take advantage of athe design-by-contract strategy for user-defined types (<code>struct</code>s orand <code>class</code>es), which can behave taggedmethods as well as member data, the programmer must tag methods as <code>const</code>, if they don't modify the object's data members. Applying the <code>const</code> qualifier to methods thusly is an essential feature for const-correctness and whileis not available in many other [[object-oriented]] languages such as [[Java (programming language)|Java]] and [[C_sharp|C#]]. While <code>const</code> methods can be called by <code>const</code> and non-<code>const</code> objects alike, non-<code>const</code> methods can only be invoked by non-<code>const</code> objects. This example illustrates:
 
<code>
class C
{
int i;
private:
int Get() const { return i; } // Note the const tag
void Set( const int j ) { i = j; }
};
 
void Foo( C& nonConstC, const C& constC )
{
int y = nonConstC.Get(); // Ok
int x = constC.Get(); // Ok: Get() is const
 
nonConstC.Set( 10 ); // Ok: nonConstC is modifiable
constC.Set( 10 ); // Error: Set() might modify constC!
}
</code>
 
Often the programmer will supply both a <code>const</code> and a non-<code>const</code> method with the same name (but possibly quite different uses) in a class to accomodate both types of callers. Consider:
 
<code>
class MyArray
{
int data[ 100 ];
private:
// ...
int& Get( const int n ) { return data[ n ]; }
int const& Get( const int n ) const { return data[ n ]; }
};
</code>
 
The <code>const</code>-ness of the calling object determines which version of <code>MyArray::Get()</code> will be invoked and thus whether or not the caller is given a reference with which he can manipulate or only observe the private data in the object. (Returning a reference to an <code>int</code> may be overkill in the second method, but the same technique can be used for arbitrary types, as in the [[Standard Template Library]].)
 
===Loop-holes to <code>const</code>-correctness===
Line 121 ⟶ 127:
Although the object <code>s</code> passed to <code>Foo()</code> is constant, which makes all of its members constant, the pointee accessible through <code>s.ptr</code> is still modifiable, though this is not generally desirable from the standpoint of <code>const</code>-correctness because <code>s</code> may solely own the pointee. For this reason, some have argued that the default for member pointers and references should be "deep" <code>const</code>-ness, which could be overridden by a <code>mutable</code> qualifier when the pointee is not owned by the container, but this strategy would create compatibility issues with existing code. Thus, for historical reasons, this loop-hole remains open in C and C++.
 
===Volatile-correctness===
===The <code>volatile</code> qualifier===
The other qualifier in C and C++, <code>volatile</code>, indicates that an object may be changed by another thread or by something external to the program at any time and so must be re-read from memory every time it is accessed. It can be used to implement a parallel design strategy, "volatile-correctness," but it is rarely used to do so. It can be used in exactly the same manner as <code>const</code> in declarations of variables, pointers, references, and member functions, but such use has little semantic value, except in the case of variablessimple objects. The <code>volatile</code> qualifier can also stripped by <code>const_cast</code>, and it can be usedcombined with the <code>const</code> qualifier as in this sample:
 
<code>
Line 150 ⟶ 156:
 
Since the address held in the <code>tableLookup</code> pointer can change implicitly, each deference might take us to a different ___location in a look-up table.
 
==Footnotes==
# [[Herb Sutter|Sutter, Herb]] and Andrei Alexandrescu (2005). ''C++ Coding Standards''. p. 30. Boston: Addison Wesley. ISBN 0321113586
 
==External links==