On 31.12.2016 01:27, Momo N wrote:
> so the question is this:
>
> //CONST int ci = 0, &cj = ci;
>
> so does the const entitle every object after its created? does this
> read "ci is a const int" and "cj is a reference to a const int"
Yes.
> as well? or is &cj not a const and just means "a reference to ci"?
It couldn't be: a reference to non-const and be bound to a const object,
as that would allow you to modify a const object.
> if the const DOES encompass the entire line, what about this...
>
> //int j = i; CONST int &k = i; int *p = &i;
>
> now that the const is in the middle, does it ignore the first object
> and create a const for everything in front of it? including the next
> object?
>
First off, C++ is case-sensitive. It has to be `const`. Unless you
defined `CONST` as a macro, to be replaced with `const` by the preprocessor.
Semicolons are much stronger delimiters than commas.
The first example is a single declaration statement, with multiple
declarators.
The second example is three declaration statements.
`const T x;` is a shorthand for `T const x`, permitted only at the very
start of the declaration. I think it's unfortunate, but all the examples
in the Holy Standard use prefix `const`. The more general notation is
needed for pointer and function declarations.
For the general notation pointer declaration, just read it backwards. :)
E.g.
char const* p;
declares `p` as a mutable pointer to `const` char, while
char c;
char* const p = &c;
declares p as a constant pointer to mutable char.
Cheers & hth.,
- Alf