C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Which way do qualifiers bind?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Daryle Walker
Guest





PostPosted: Fri Jul 30, 2004 9:46 am    Post subject: Which way do qualifiers bind? Reply with quote



You should know that this:

MyType * x, y;

declares "x" to be a "MyType *" and "y" to be a "MyType". In other
words, the "*" binds with the "x".

But what about:

MyType const * x, * y;

Are both "x" and "y" of "MyType const *", or is only "x" of that type
and "y" gets "MyType *"? Does the "const" (and/or "volatile") stick
with the type or the object?

(And how does it work with multiple levels like:

MyType const * volatile * x, **y;

?)

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Jul 30, 2004 3:31 pm    Post subject: Re: Which way do qualifiers bind? Reply with quote



In article
<dwalker07-B71D30.22455629072004 (AT) newsclstr01 (DOT) news.prodigy.com>, Daryle
Walker <dwalker07 (AT) snet (DOT) net> writes
Quote:
You should know that this:

MyType * x, y;

declares "x" to be a "MyType *" and "y" to be a "MyType". In other
words, the "*" binds with the "x".

But what about:

MyType const * x, * y;

Are both "x" and "y" of "MyType const *", or is only "x" of that type
and "y" gets "MyType *"? Does the "const" (and/or "volatile") stick
with the type or the object?

(And how does it work with multiple levels like:

MyType const * volatile * x, **y;

?)

Of course none of this matters if you follow good practice and restrict
declarations to one per statement:-) FWIW const & volatile bind to the
type on the left (or that on the immediate right if there is nothing on
the left.) So in your last example x is a pointer to a volatile pointer
to const MyType object and y is a pointer to a pointer to a const Mytype
object.

However such code would never pass any code review in which I was
concerned.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Sat Jul 31, 2004 3:05 am    Post subject: Re: Which way do qualifiers bind? Reply with quote



Daryle Walker wrote:
Quote:
You should know that this:

MyType * x, y;

declares "x" to be a "MyType *" and "y" to be a "MyType". In other
words, the "*" binds with the "x".

But what about:

MyType const * x, * y;

Are both "x" and "y" of "MyType const *", or is only "x" of that type
and "y" gets "MyType *"? Does the "const" (and/or "volatile") stick
with the type or the object?

It sticks with the type of the object.

Simple declarations begin with a sequence of declaration specifiers
followed by a comma-separated sequence of declarators:

- Declaration specifiers (section 7.1 of the standard) include
existing type names and keywords such as static, extern, inline,
typedef, friend, and const. There must be exactly one existing
type name in the sequence.

- Declarators (section Cool are the parts that give names to the
declarations and include the syntactical decoration that makes
pointer, reference, function and array types.

All the declaration specifiers apply to each of the declarators.

"*" isn't a declaration specifier, so the declaration specifier
sequence is "MyType const" and the declarators are "* x" and "* y".
This declares MyType const * x and MyType const * y.

Quote:
(And how does it work with multiple levels like:

MyType const * volatile * x, **y;

?)

"*" isn't a declaration specifier, so the declaration specifier
sequence is "MyType const" and the declarators are "* volatile * x"
and "**y". This declares MyType const * volatile * x and MyType const
** y.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
gorda
Guest





PostPosted: Sat Jul 31, 2004 3:13 am    Post subject: Re: Which way do qualifiers bind? Reply with quote

Daryle Walker <dwalker07 (AT) snet (DOT) net> wrote

Quote:
You should know that this:

MyType * x, y;

declares "x" to be a "MyType *" and "y" to be a "MyType". In other
words, the "*" binds with the "x".


Yes that seems correct.

Quote:
But what about:

MyType const * x, * y;

Are both "x" and "y" of "MyType const *", or is only "x" of that type
and "y" gets "MyType *"? Does the "const" (and/or "volatile") stick
with the type or the object?


x is of type: MyType const *
y is of type: Mytype const *

Quote:
(And how does it work with multiple levels like:

MyType const * volatile * x, **y;

?)

The way I understand it, all the qualifiers apply to all the variables
UNTIL you come across a '*' or a variable name. The '*' and anything
after the '*' applies to the specific variable alone. So for the above
case,
x is of type: MyType const * volatile *
y is of type: MyType const **y;

Another example:
MyType const x, * const * y, * const z, w;
x is of type: MyType const
y is of type: MyType const * const *
z is of type: MyType const * const
w is of type: MyType const


Also it appears that anything after a comma has to be either a
variable name or a '*', but there could be exceptions I am unaware of.

This is incorrect:
MyType const *x, volatile y; //Compiler indicates "parse error before
volatile"

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
White Wolf
Guest





PostPosted: Sun Aug 01, 2004 2:13 am    Post subject: Re: Which way do qualifiers bind? Reply with quote

Francis Glassborow wrote:
[SNIP]
Quote:
MyType const * volatile * x, **y;
[SNIP]
the left.) So in your last example x is a pointer to a volatile pointer
to const MyType object and y is a pointer to a pointer to a const Mytype
object.

Now if we are picky... isn't that right that the final MyType object the
final pointer in the type of x points to is not a const object? Isn't
rather the pointer pointing to it, which only allows const access?

I have no idea how to word it in English to make the difference between a
const pointer - as a pointer which cannot be changed to point to something
else - versus const pointer - as a pointer which cannot be used to change
what it points to. Changing the former to pointer constant seem to be one
way, but that might suggest wrong ideas as people may mix it with pointer
literals. Although in 9 AD (9 years after DOS) it is significantly less
usual to hack with direct addresses.

I am asking/telling this, because I do find it difficult to express the
difference between the two - in English. I have the same trouble with the
term "const pointer" as the trouble was with "template class". Too many
possible meanings... And as the terms (both of them) is to be used in
everyday (cafeteria) conversations it would be nice to find short terms for
them, const qualified pointer will just not do. The milk would go sour
during the conversation.

Seriously, in teaching people I feel the need to find good terms for these
two concepts - one which does not hold the false meaning of the pointed
object being const (IOW ROM) object.

Quote:
However such code would never pass any code review in which I was
concerned.

You are cruel! Smile IMHO the guy just wanted to know. Some people are trying
to understand how an autist's mind works, but they do not really want to
think like one. ;-)

--
WW aka Attila
:::
Business - the art of extracting money from another man's pocket without
resorting to violence. -- Max Amsterdam



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Aug 01, 2004 2:20 pm    Post subject: Re: Which way do qualifiers bind? Reply with quote

In article <ceh6t5$dgr$1 (AT) phys-news1 (DOT) kolumbus.fi>, White Wolf
<wolof (AT) freemail (DOT) hu> writes
Quote:
Francis Glassborow wrote:
[SNIP]
MyType const * volatile * x, **y;
[SNIP]
the left.) So in your last example x is a pointer to a volatile pointer
to const MyType object and y is a pointer to a pointer to a const Mytype
object.

Now if we are picky... isn't that right that the final MyType object the
final pointer in the type of x points to is not a const object? Isn't
rather the pointer pointing to it, which only allows const access?

No, read more carefully. Removing the confusing declaration of x we get:

MyType const **y;

Whereas what you were reading is :

MyType * const * y;

It is that pesky allowance of:

const MyType **y;

that is confusing you.

Remember that unless it comes first, const (and volatile) qualify the
type to the left.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Mon Aug 02, 2004 10:22 am    Post subject: Re: Which way do qualifiers bind? Reply with quote

"White Wolf" <wolof (AT) freemail (DOT) hu> writes:

Quote:
Francis Glassborow wrote:
[SNIP]
MyType const * volatile * x, **y;
[SNIP]
the left.) So in your last example x is a pointer to a volatile pointer
to const MyType object and y is a pointer to a pointer to a const Mytype
object.

Now if we are picky... isn't that right that the final MyType object the
final pointer in the type of x points to is not a const object? Isn't
rather the pointer pointing to it, which only allows const access?

I have no idea how to word it in English to make the difference between a
const pointer - as a pointer which cannot be changed to point to something
else - versus const pointer - as a pointer which cannot be used to change
what it points to.
[snip]


A pointer which cannot be used to change what it points must properly
be called 'a pointer to const' and not 'a const pointer' - those
who call it the latter are guilty of spreading confusion.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Mon Aug 02, 2004 10:25 am    Post subject: Re: Which way do qualifiers bind? Reply with quote

Daryle Walker <dwalker07 (AT) snet (DOT) net> wrote

Quote:
You should know that this:

MyType * x, y;

declares "x" to be a "MyType *" and "y" to be a "MyType". In other
words, the "*" binds with the "x".

But what about:

MyType const * x, * y;

Are both "x" and "y" of "MyType const *", or is only "x" of that type
and "y" gets "MyType *"? Does the "const" (and/or "volatile") stick
with the type or the object?

(And how does it work with multiple levels like:

MyType const * volatile * x, **y;

?)

The topic was always confusing to me. I am specially interested in two
advices: one from Francis Glassborrow and the other from Ben
Hatchings. I just wander how to combine the two advices.

There are two interpretations for:
int * i;

1. int (*i); // *i is int.

2.( int*) i; // i is int* (i.e., pointer to int)

The first interpretation is a traditional C way ( let me call it C
style interpretation hereafter).

The second interpretation is supported by Bjarne Stroustrup ( if I am
right) ( let me call it C++ style interpretation hereafter).

When you see the code
Quote:
MyType * x, y;,
the C style interpretation seems to be better, because the code means

Mytype (*x), y;.
This is because the comma syntax has been used since one of very
beginning days of C.

What does Bjarne Stroustrup say about the syntax. He just says "Don't
use it. It is confusing."

I rarely use the comma syntax according the advice of Bjarne
Stroustrup, but you cannot stop other persons using the comma syntax,
as the syntax is defined in the Standard as Ben Hatchings pointed out.

A practical way you can take when you encounter a comma syntax for
declaration is:

1. to analyze the syntax by the C style interpretation and according
to the advice from Ben Hatchings.
2. and then to analyze the semantic by the C++ style interpretation
and according to the advice from Francis Glassborrow.

For example
Quote:
MyType const * volatile * x, **y;

1. Syntax analyze:
(MyType const) (* volatile * x), (**y);
(1) (MyType const) (* volatile * x);
(2) (MyType const) (**y);

2. Sematic analyze:
(1) MyType const * volatile * x;
Reading from left to right,
x is pointer to volatile pointer to const MyType.
(2) MyType const **y;
Reading from left to right,
y is pointer to pointer to const MyType.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Kanze
Guest





PostPosted: Sun Aug 08, 2004 10:18 pm    Post subject: Re: Which way do qualifiers bind? Reply with quote

"White Wolf" <wolof (AT) freemail (DOT) hu> writes:

Quote:
Francis Glassborow wrote:
[SNIP]
MyType const * volatile * x, **y;
[SNIP]
the left.) So in your last example x is a pointer to a volatile
pointer to const MyType object and y is a pointer to a pointer to
a const Mytype object.

Now if we are picky... isn't that right that the final MyType object
the final pointer in the type of x points to is not a const object?
Isn't rather the pointer pointing to it, which only allows const
access?

I have no idea how to word it in English to make the difference
between a const pointer - as a pointer which cannot be changed to
point to something else - versus const pointer - as a pointer which
cannot be used to change what it points to.

The problem is not your English, but ours. There are const pointers,
and pointers to const, but for various histeri^H^H^Horical reasons, when
we mean a pointer to const, we often say const pointer.

In the above declaration:

x is a pointer to a volatile pointer to a const MyType
y is a pointer to a pointer to a const MyType

In both cases, the MyType object is const, and not the pointers.

The grammar is a bit tricky, and as Francis said, the easiest and the
best solution is to only declare one object per declaration -- that way,
you don't have any problems wondering whether a const applies to the
whole declaration, or simply to the first object.

Quote:
Changing the former to pointer constant seem to be one way, but that
might suggest wrong ideas as people may mix it with pointer
literals. Although in 9 AD (9 years after DOS) it is significantly
less usual to hack with direct addresses.

I am asking/telling this, because I do find it difficult to express
the difference between the two - in English.

Don't feel bad. The problem isn't your English; it is our misuse of
English. In English, a const pointer is a pointer which cannot be
changed; the expression "const pointer" says nothing about what is being
pointed to. So in:

const int* pi ;

pi is NOT a const pointer. (It is a pointer to const.) Regretfully, I
think you'll find a lot of books and articles which call it a const
pointer.

Note that the abuse is even worse with references. In plain English,
all references are const. But of course, everyone (including myself)
speaks of const references for T const& -- in English this should be a
reference to const. But in C++'ish...

Quote:
I have the same trouble with the term "const pointer" as the trouble
was with "template class".

Again, the problem is us, not you. A template class is NOT a class
template, but we often use the two interchangeably. (And what about a
null pointer constant, which can be many things, but in no case a
pointer.)

English doesn't help much here, you have to learn C++'ish.

Quote:
Too many possible meanings... And as the terms (both of them) is to
be used in everyday (cafeteria) conversations it would be nice to
find short terms for them, const qualified pointer will just not do.
The milk would go sour during the conversation.

The "correct" terms are "const pointer" (for T *const) and "pointer to
const" (for T const*). In practice, I would always qualify the
difference somehow in the first case -- far too often will T const* be
referred to as a const pointer.

So don't feel bad about it. My mother was American, and I grew up
speaking English, and the usage confuses me.

Quote:
Seriously, in teaching people I feel the need to find good terms for
these two concepts - one which does not hold the false meaning of
the pointed object being const (IOW ROM) object.

In teaching, I would start by pointing out the incorrect common usage,
and insist on the correct usage in my class. In English, adjectives
precede what they modify, so a const pointer is a T*const. In C++,
"adjectives" like const and volatile follow what they modify, so we use
T *const for a const pointer, and T const* for a pointer to const.

At least the order is systematic in both cases. The rule in French is
that the adjective follows the noun, except when it precedes it. (Oh
well, it allowed a beautiful joke in "Asterix chez les Bretons". Joke
which I bet is lost in every translation.)

Quote:
However such code would never pass any code review in which I was
concerned.

You are cruel! Smile IMHO the guy just wanted to know. Some people are
trying to understand how an autist's mind works, but they do not
really want to think like one. Wink

Quite agreed. There are a lot of things that wouldn't pass my code
reviews, but that I have to understand, because some people have the
audacity to not have their code reviewed by me.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.