co******@yahoo.co.uk (CoolPint) wrote in message news:<15**************************@posting.google. com>...
Read each token in the declaration from right to left.
Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?
What is const int * & ?
Is it a constant reference to a pointer to an integer? Or
Is it a reference to a pointer to a constant integer?
What is being constant in this case? The pointer or the integer being pointed?
Lets make it a proper declaration.
const int * & i;
From right to left: "i is a reference to a pointer to a constant int".
Except I've broken my right to left rule here by reading the tokens in
the order
&
*
const
int
See below for the explanation, after your third example.
How about int * const & ?
Is this a reference to a constant pointer to an integer? Or
Is this a constant reference to a pointer to an integer
What is being constant in this case?
int * const & i;
"i is a reference to a constant pointer to an int". No problems ;-)
What about int const * & ?
Strangely enough, this compiles. What is this?
And what is being constant in this case?
int const * & i;
Right to left again: "i is a reference to a pointer to a constant
int". Now that's exactly the same as the first one. What I think is
confusing you is this - leaving pointers and references out of it, are
you aware that
const int i;
and
int const i;
both mean the same thing? The only difference is that the second one
makes sense reading from right to left ("i is a constant int"). For
this reason some people prefer to always put the type before the
const. Whenever const and a type name (int in your examples) appear
next to each other they can appear in either order. So
const int * & i;
int const * & i;
both mean "i is a reference to a pointer to a constant int".
How about the following cases?
const int * const &
const int * const & i;
"i is a reference to a constant pointer to a constant integer".
This could also be written
int const * const & i;
which reads better right to left.
const int const * & g++ says "duplicate constant"
const int const * & i;
"i is a reference to a pointer to a ... errrm constant int constant
???" What's a constant int constant? Remove one of the consts and you
have either
int const * & i;
or
const int * & i;
which we've seen above both mean the same thing. Since both occurances
of const in your declaration would do the same thing if the other
const wasn't there, you are getting a message about duplicate const.
I would very much appreciate clear explanation on these. Thank you in advance.
From very confused.
I hope you are less confused now :)
GJD