Content deleted Content added
TakuyaMurata (talk | contribs) :{{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.
===Simple data types===
<code>▼
</code>▼
<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</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:▼
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!▼
}▼
===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
▲<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 other qualifier in C and C++, <code>volatile</code>, indicates that an object may be changed
<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==
|