469,317 Members | 1,985 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

Beginner's question: What is "const int * &"?

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?

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?

What about int const * & ?
Strangely enough, this compiles. What is this?
And what is being constant in this case?

How about the following cases?

const int * const &
const int const * & g++ says "duplicate constant"

I would very much appreciate clear explanation on these. Thank you in advance.

From very confused.
Jul 19 '05 #1
2 15988
For a good text on good usage of const, look at:

http://users.utu.fi/~sisasa/oasis/cp...rrectness.html

/ Martin
Jul 19 '05 #2
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by zealotcat | last post: by
4 posts views Thread by barney | last post: by
5 posts views Thread by martin | last post: by
2 posts views Thread by Rouben Rostamian | last post: by
14 posts views Thread by Arne | last post: by
6 posts views Thread by Darin Johnson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.