Escolar Documentos
Profissional Documentos
Cultura Documentos
http://en.wikipedia.org/wiki/Const-correctness
*ptrToConst = 0; // Error! Cannot modify the "pointee" data ptrToConst = 0; // OK: modifies the pointer *constPtr = 0; // OK: modifies the "pointee" data constPtr = 0; // Error! Cannot modify the pointer *constPtrToConst = 0; // Error! Cannot modify the "pointee" data constPtrToConst = 0; // Error! Cannot modify the pointer } To render the syntax for pointers more comprehensible, a rule of thumb is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointee type and everything to the right of the star are the pointer properties. (For instance, in our example above, int const * can be read as a mutable pointer that refers to a non-mutable integer, and int * const can be read as a non-mutable pointer that refers to a mutable integer.) References follow similar rules. A declaration of a const reference is redundant since references can never be made to refer to another object: int i = 42; int const & refToConst = i; // OK int & const constRef = i; // Error the "const" is redundant Even more complicated declarations can result when using multidimensional arrays and references (or pointers) to pointers; however, some have argued that these are confusing and error-prone and that they therefore should generally be avoided or replaced with higher-level structures. C/C++ also allows the following syntax: const int* ptrToConst; //identical to: int const * ptrToConst, const int* const constPtrToConst;//identical to: int const * const constPtrToConst
2 of 8
24/05/12 16:20