What's your preferred pointer declaration style, and why?

Posted by Owen on Stack Overflow See other posts from Stack Overflow or by Owen
Published on 2008-12-18T08:06:31Z Indexed on 2010/04/18 1:03 UTC
Read the original article Hit count: 292

Filed under:
|
|
|

I know this is about as bad as it gets for "religious" issues, as Jeff calls them. But I want to know why the people who disagree with me on this do so, and hear their justification for their horrific style. I googled for a while and couldn't find a style guide talking about this.

So here's how I feel pointers (and references) should be declared:

int* pointer = NULL;
int& ref = *pointer;
int*& pointer_ref = pointer;

The asterisk or ampersand goes with the type, because it modifies the type of the variable being declared.

EDIT: I hate to keep repeating the word, but when I say it modifies the type I'm speaking semantically. "int* something;" would translate into English as something like "I declare something, which is a pointer to an integer." The "pointer" goes along with the "integer" much more so than it does with the "something." In contrast, the other uses of the ampersand and asterisk, as address-of and dereferencing operators, act on a variable.

Here are the other two styles (maybe there are more but I really hope not):

int *ugly_but_common;
int * uglier_but_fortunately_less_common;

Why? Really, why? I can never think of a case where the second is appropriate, and the first only suitable perhaps with something like:

int *hag, *beast;

But come now... multiple variable declarations on one line is kind of ugly form in itself already.

© Stack Overflow or respective owner

Related posts about subjective

Related posts about c++