views:

81

answers:

6

Possible Duplicate:
What’s your preferred pointer declaration style, and why?

Just curious: What's you standard method when declaring pointers? Why?

SomeClass *instance;

OR

SomeClass* instance;

I prefer the second method as it keeps the entire type together (it's a 'SomeClass pointer'). But I know many prefer the first and I'm just curious as to why.

+1  A: 

Both of them. I.e:

SomeClass* instance;

but

SomeClass *instance1, *instance2, *instance3;

P.S. IMO, it is an unsignificant, minor detail that isn't worth the attention.

SigTerm
A: 

I mix the two. Different codebases I work in have different conventions. I think I prefer the second, because if you say the type out loud, it is "Some Class Star", and putting that all together makes reading the code easier.

On a previous project, we weren't allowed to use Class*, we had to typedef Class* ClassPtr, which was just silly. Don't do that.

Adam Shiemke
+3  A: 

The first form is used because that's how C (and by extension C++ and Objective C) parses the type. Consider:

SomeClass obj, *pobj;

If you follow the common convention of one variable per declaration, it doesn't really matter which form you use.

Cogwheel - Matthew Orlando
A: 

I prefer to separate type and variable name:

SomeClass* instance;

and ban declaration of mixed types or multiple pointer declaration on the same line:

SomeClass obj, *pobj;
SomeClass *instance1, *instance2, *instance3;

so there is no need for mixed usage.

If I work on code in which other style is used, I adapt and keep it: Consistency trumps style.

MaR
A: 

Consider the reasoning of the designer of C++. There are valid arguments for both styles, but arguments in favor of Type *var are based on syntax and arguments in favor of Type* var are based on semantics. Making the semantic meaning of your code clear seems more important to me.

Larry Engholm
A: 

I use type* var in C++, and type *var in C.

Pavel Minaev