|
From: | Rik |
Subject: | Re: convention for declaring pointers? |
Date: | Mon, 24 Apr 2017 08:47:39 -0700 |
On 04/24/2017 08:32 AM, Rik wrote: > On 04/23/2017 10:40 PM, Mike Miller wrote: >> On Sun, Apr 23, 2017 at 22:15:27 -0700, Rik wrote: >>> jwe, >>> >>> Is there a convention for whether to cuddle the '*' for pointers with the >>> variable name, variable type, or neither? I noticed in >>> liboctave/array/idx-vector.h declarations such as the following >>> >>> idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); >>> >>> I started to change this to >>> >>> idx_range_rep *r = ... >>> >>> but then I realized that maybe the extra space is good. It is consistent >>> with the rest of Octave coding conventions which tends to emphasize extra >>> whitespace for readability. The space between the name of a function and >>> the opening parenthesis is an example of this openness. >> I've always seen the style >> >> idx_range_rep *r = ... >> >> preferred, and I think we pretty consistently prefer that in Octave so >> far. It also helps emphasize that the '*' is required for each >> declaration in a compound declaration like >> >> octave_idx_type *ridx, *cidx; >> >> GNU indent also corrects the string "int * x" to "int *x". > > This is usually the style I have seen as well. What got me thinking about > the alternative construction was the analogy to nouns and adjectives in the > English language. > > The declaration > > int var; > > is made of of a type (int, or noun) and a name (var). Now, if you add > modifiers they are generally associated with the noun rather than the > name. This works in C++ too as the type can have several modifiers. > > unsigned int var; > unsigned short int var; > > Finally, if something is a pointer one would say in English something like > "var is a pointer to unsigned int". If one could, the natural coding would be > > * unsigned int var; > > But, that's not legal syntax so the question was whether having all the > adjectives separated > > unsigned int * var; > > might be clearer than > > unsigned int *var > > Of course, names are proper nouns in English and can also be modified by > adjectives, for example, "the treacherous Lady Macbeth". > > I don't have a dog in this race and am happy to use either. Just for fun, I looked at the Cplusplus.com tutorial and they code it with the space. int * number; char * character; float * greatnumber; And I looked at the C++11 FAQ on Stroustrup's own website and his examples use the other convention class Arena { void* p; int s; public: Arena(void* pp, int ss); Obviously, we should do whatever we want. --Rik |
[Prev in Thread] | Current Thread | [Next in Thread] |