Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

#1NEWBIEHERE question about "const" before multiple commas and objects

36 views
Skip to first unread message

Momo N

unread,
Dec 30, 2016, 7:28:08 PM12/30/16
to
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" as well? or is &cj not a const and just means "a reference to ci"?

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?

Alf P. Steinbach

unread,
Dec 30, 2016, 7:51:41 PM12/30/16
to
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

woodb...@gmail.com

unread,
Jan 1, 2017, 5:42:59 PM1/1/17
to
Right-to-left is forwards for Arabic, Hebrew and some other languages.


Brian
Ebenezer Enterprises
http://webEbenezer.net

Jorgen Grahn

unread,
Jan 2, 2017, 12:03:28 PM1/2/17
to
On Sat, 2016-12-31, 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" as
> well? or is &cj not a const and just means "a reference to ci"?

Note that very few people write code like that. I associate it with C
code written in the 1980s; it's less common in contemporary C code,
and AFAICT much less common in C++.

Your question is valid, but it's not an important one. I may be
wrong, but I get the impression that you should focus on other
things. If you need an answer, the easiest way to get it is to write
an example program and see if it compiles -- you can trust your
compiler in this case.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages