7.6 DECLARING MULTIPLE NAMES


7.6 DECLARING MULTIPLE NAMES

It is possible to declare several names in a single declaration. The declaration simply contains a list of comma-separated declarators:

       int x, y, v[10]; in C++ or Java 

is equivalent to

       int x;       int y;       int v[10]; 

Similarly,

       float[] x, y; in Java 

is equivalent to

       float[] x;       float[] y; 

Declaring several names in a single statement can sometimes lead to unreadable code. For example, you could say in C++

       int* p, y; 

If you have become accustomed to thinking of int* as a type specifier, you'd think that the above declaration is equivalent to

        int* p;        int* y; 

But that is wrong, because, syntactically speaking, the symbol * is a unary operator that is right associative. Therefore, when the compiler sees

        int* p, y; 

it actually reads

       int* p, y; 

which is how you'd write it in a C program anyway. But to quickly assimilate a program visually it is better to write a pointer declaration as "int* p;" as opposed to "int *p;," even though the two are completely equivalent. Thinking of int* as a pointer type, you immediately see p as a pointer to an integer variable. In any case, regardless of your preferences, in C++ it has now become common to declare a pointer in the fashion exemplified by "int* p;."




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net