const char *&

M

Marcin Kalicinski

void f(const char *&text);

Is this a const reference to char * or a reference to const char *? And how
to write both of them?

thank you,
Marcin
 
J

John Carson

Marcin Kalicinski said:
void f(const char *&text);

Is this a const reference to char * or a reference to const char *?
And how to write both of them?

You read from right to left, so it is a reference to a pointer to const
char. All references are const in the sense that you cannot "reseat" them to
refer to a different object. Your only choices in the present context are
between

1. a reference to a pointer to const char, as above,

const char *&text

2. a reference to a const pointer to char, which is

char *const &text

3. a reference to a const pointer to const char, which is

const char *const &text

Note that const char in 1. and 3. is the same as char const
 
J

jefong

1,const int * a=&b;
when "const" on the left "*",specifies a const variable that is
referenced by a.
2.int * const a=&b;
when "const" on the right "*",specifies a const pointer.
 
K

Karl Heinz Buchegger

1,const int * a=&b;
when "const" on the left "*",specifies a const variable that is
referenced by a.
2.int * const a=&b;
when "const" on the right "*",specifies a const pointer.

What about

int const * a = &b;

?

The thing is: Your 'rule' is mighty complicated.
Much simpler:

const always works on the thing to its left.
Unless const is the leftmost specifier, in which case
it works on the thing immediatly to its right.

So, in

const int * a

the const works on the thing immediatly to its right:
It is the int, that is const

int * const a

const is not the leftmost specifier, thus it works on the thing
to its left: it is the pointer that is const.

int const * a

const is not the leftmost specifier, thus it works on the thing
to its left: it is the int that is const.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top