Re: const qualifier and VC6.0

From: Andrew Morgan (Andrew.Morgan_at_telkomsa.net)
Date: 10/07/03


Date: Tue, 07 Oct 2003 15:44:40 +0200

tom_usenet wrote:

> On Tue, 07 Oct 2003 13:12:00 +0200, Andrew Morgan
> <Andrew.Morgan@telkomsa.net> wrote:
>
>
>>Since the * operator is unary on the following address, it is better
>>style (uh-oh ;-)) to write * with the variable than with the type:
>>int *a,*b;
>
>
> 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



Relevant Pages

  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... int a=10; ... Not in his declaration it didn't. ...
    (comp.lang.c)
  • Re: C# prototype of C: void**
    ... public unsafe void Capture (int width, int height, bytebuffer) ... but I got the error message "A ref or out argument must be an lvalue". ... declaration in the parameter list of Capture allows changing of the ... > have a pointer to a pointer, you need to pass it by ref. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: would C be easier to read if...
    ... That indicates that it's a pointer to a function. ... int *a a is pointer to int ... In my case a type declaration that reads linearly from left to right would ... Let's say we have a regular int identifier named "foo". ...
    (comp.lang.c)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... main returns an int. ... The 'a' in fun() has no declaration and your compiler ought to have told you so. ...
    (comp.lang.c)
  • Re: exporting function with parm pointer to struct
    ... a structure field" when I call the function. ... public static extern int z9indqry; ... > you not use a pointer to the struct, but rather, a pointer to void in the ... > redefine the C# code (at least for the declaration). ...
    (microsoft.public.dotnet.languages.csharp)