Clarifying 'const' terminology

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jul 8 13:16:51 PDT 2006


Bruno Medeiros wrote:
> There are some variations in const semantics. In C++, const is a
> type-modifier, meaning that const can be applied to each type element,
> and not the "variable as whole". Best to give an example. In C++ you can
> have:
>   const int * iptr = ...;
>   int const * iptr = ...;
>   const int const * iptr = ...;
> each with different meanings:
> 
>   const int * iptr = ...;
>   // cannot modify the pointed int, can modify the pointer
> 
>   int const * iptr = ...;
>   // can modify the pointed int, cannot modify the pointer (the pointer
> is const)
> 
>   const int const * iptr = ...;
>   // Cannot modify the pointed int, nor cannot modify the pointer
> 

Actually, the former two are equivalent. To clarify (equivalent types are paired
on one line):

const int, int const
const int *, int const *
const int * const, int const * const
int * const

There's only one way to write that last one: a constant pointer to a
non-constant integer.

This is why I always write "const" second when writing C(++): it'd be
inconsistent with the way const pointers are written out to do otherwise. Plus,
they can be read right-to-left more easily. ("const int * const" reads as
"constant pointer to an integer --- no, wait, a constant integer" to me <g>)



More information about the Digitalmars-d-learn mailing list