Content deleted Content added
re: const references. |
Major rewrite. |
||
Line 1:
'''<code>Const
When a <code>const</code> object is used, the compiler will do its best to prevent the object from being changed, which allows programmers to utilize a simple form of [[design by contract|design contract]]. The idea of <code>const</code>-ness does not imply that the variable as it is stored in the [[computer]]'s [[computer storage|memory]] is unwriteable. Rather, <code>const</code>-ness is a compile-time construct that indicates what a programmer ''may'' do, not necessarily what he ''can'' do. (Of course, if an object resides in a [[Read-only memory|ROM]], then it is physically unwriteable regardless of its <code>const</code>-ness from the perspective of the [[compiler]].)
A [[class method]] can be declared as const, indicating that calling that method does not change the object. Const methods can only call other const methods, but cannot assign [[field (computer science)|member variables]]. (In C++, a member variable can be declared as <code>mutable</code>, indicating that a const method can change its value. This can be used for [[cache|caching]] and [[reference counting]], where the meaning of the object is unchanged, but the object is not [[bitwise const]].)▼
▲A [[class method]] can be declared as <code>const</code>, indicating that calling that method does not change the object.
==C/C++ Syntax==
For simple types, the
<code>
</code>
is equivalent to
<code>
</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 foo</code>) generates a warning but not an error.
===Pointers and references===
For pointer and reference types, the syntax is slightly more subtle. A pointer object can be declared as a <code>const</code> pointer or a pointer to a <code>const</code> object (or both). A <code>const</code> pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the object that it points to (called the "pointee"). (Reference variables are thus an alternate syntax for <code>const</code> pointers.) A pointer to a <code>const</code> object, on the other hand, can be reassigned to point to another object of the same type or of a convertible type, but it cannot be used to modify any object. A <code>const</code> pointer to a <code>const</code> object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object. The following code illustrates these subtleties:
<code>
void Foo( int *ptr, int const * ptrToConst, int *const constPtr, int const * const constPtrToConst )
{
int const i;
*ptr = 0; // Ok: modifies the pointee
ptr = &i; // Ok: modifies the pointer
*ptrToConst = 0; // Error! Cannot modify the pointee
ptrToConst = &i; // Ok: modifies the pointer
*constPtr = 0; // Ok: modifies the pointee
constPtr = &i; // Error! Cannot modify the pointer
*constPtrToConst = 0; // Error! Cannot modify the pointee
constPtrToConst = &i; // Error! Cannot modify the pointer
}
</code>
To render the syntax for pointers more comprehensible, a [[rule of thumb]] is to read the declaration from right to left. Thus, everything before the star can be identified as the pointee type and everything to the left are the pointer properties. (For instance, in our example above, <code>constPtrToConst</code> can be read as a <code>const</code> pointer that refers to a <code>const int</code>.)
References follow similar rules. A declaration to a <code>const</code> reference is permitted for the sake of templates but is technically redundant since references can never be made to point to another object:
<code>
int i = 42;
int const & refToConst = i; // Ok
int & const constRef = i; // Ok, but const is redundant here
</code>
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.
===Loop-holes to <code>const</code>-correctness===
There are two loop-holes to pure <code>const</code>-correctness in C and C++ which exist for compatibility with existing code.
The first, which applies only to C++, is the use of <code>const_cast</code>, which allows the programmer to strip the <code>const</code> qualifier, making any object modifiable. The necessity of stripping the qualifier arises when using existing code and libraries that cannot be modified but which are not <code>const</code>-correct. For instance, consider this code:
<code>
// Prototype for a function we cannot change
void LibraryFunc( int * ptr, int size );
void CallLibraryFunc( int const * const ptr, int const size )
{
LibraryFunc( ptr, size ); // Error! Drops const qualifier
int *const nonConstPtr = const_cast<int*>( ptr ); // Strip qualifier
LibraryFunc( nonConstPtr, size ); // Ok
}
</code>
The other loop-hole applies both to C and C++. Specifically, the languages dictate that member pointers and references are "shallow" with respect to the <code>const</code>-ness of their owners — that is, a containing object that is <code>const</code> has all <code>const</code> members except that member pointees (and referees) are still mutable. To illustrate, consider this code:
<code>
struct S
{
int val;
int * ptr;
};
void Bar( struct S const s )
{
int i = 42;
s.val = i; // Error: s is const, so val becomes a const int
s.ptr = &i; // Error: s is const, so ptr becomes a const pointer
*s.ptr = 0; // Ok: the pointee known by s is always mutable,
// though this is usually not desirable
}
</code>
Although the structure <code>s</code> passed to <code>Bar()</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++.
===<code>volatile</code>===
The other qualifier in C and C++, <code>volatile</code>, can be used in exactly the same manner as <code>const</code> in declarations of variables, pointers, references, and member functions, but, except in the case of variables, such use has little semantic value. The <code>const_cast</code> keyword can also be used to strip the <code>volatile</code> qualifier.
[[Category:C programming language family]]
|