Const (computer programming): Difference between revisions

Content deleted Content added
m C/C++ Syntax: to "C++ syntax"
Line 128:
 
===Volatile-correctness===
The other qualifier in C and C++, <code>volatile</code>, indicates that an object may be changed by something external to the program at any time and so must be re-read from memory every time it is accessed. ItThe canqualifier beis usedmost tooften implementfound ain parallelembedded designsystems strategy,or "volatile-correctness,"systems butmanipulating ithardware is rarely used to do sodirectly. 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 simple objects. (In fact, <code>volatile</code> could be used to implement a similar design-by-contract strategy which might be called <code>volatile</code>-correctness, but it is almost never used to do so.) The <code>volatile</code> qualifier can also stripped by <code>const_cast</code>, and it can be combined with the <code>const</code> qualifier as in this sample:
 
<code>
Line 141:
</code>
 
Because <code>hardwareRegister</code> is volatile, there is no guarantee that it will hold the same value on two successive reads even though the programmer cannot modify it. The semantics here indicate that value is read-only but not necessarily unchanging.

We can also create volatile pointers, though their applications are rarer:
 
<code>
Line 155 ⟶ 157:
</code>
 
Since the address held in the <code>tableLookup</code> pointer can change implicitly, each deference might take us to a different ___location in a memory-mapped look-up table.
 
==Footnotes==