tom_usenet said:
The inevitable bite:
In C++ it is better to put the * with the type, since in C++ the
emphasis is on types, while in C the emphasis is on expressions. It is
also important that you only use one pointer or reference declaration
per line.
This is a self-imposed restriction, since the compiler could not care
less about:
int a,*b,**c;
int* p, j; //bad!
int* i; //good
int j; //good
I have no intentional of being drawn into a controversy, so my only
response will be to quote verbatim from "C++ The Complete Reference, 3rd
Edition" by Herbert Schildt:
"A Matter of Style
------------------
When declaring pointer and reference variables, some C++ programmers use
a unique coding style that associates the * or the & with the type name
and not the variable. For example, here are two functionally equivalent
declarations:
int& p; // & associated with type
int &p; // & associated with variable
Associating the * or & with the type name reflects the desire of some
programmers for C++ to contain a separate pointer type. However, the
trouble with associating the & or * with the type name rather than the
variable is that, according to the formal C++ syntax, neither the & nor
the * is distributive over a list of variables. Thus, misleading
declarations are easily created. For example, the following declaration
creates one, not two, integer pointers.
int* a,b;
Here, b is declared as an integer (not an integer pointer) because,
as specified by the C++ syntax, when used in a declaration, the * (or &)
is linked to the individual variable that it precedes, not to the type
that it follows. The trouble with this declaration is that the visual
message suggests that both a and b are pointer types, even though, in
fact, only a is a pointer. This visual confusion not only misleads
novice C++ programmers, but occasionally old pros, too.
It is important to understand that, as far as the C++ compiler is
concerned, it doesn't matter whether you write int *p or int* p. Thus
if you prefer to associate the * or & with the type rather than the
variable, feel free to do so. However, to avoid confusion, this book
will continue to associate the * and the & with the variables that they
modify rather than their types."
(FWIW, Stroustrup uses this style)
Tom
It is simply a matter of style I and do not think that people should be
berated for a style preference. FWIW many of the great C++ authors like
Scott Meyers, Herbert Schildt and James Coplien use the int *a style,
which others like Herb Sutter, Nicolai Josuttis and Andrei Alexandrescu
use int* a.
Andrew