views:

80

answers:

1

Which is the better convention of declaring pointers in C++?

MyClass* ptr

(or)

MyClass *ptr

I find the first one meaningful, because i feel like declaring MyClass pointer rather than a MyClass and specifying a type modifier. But i see a lot of books recommending the later convention. Can you give the rational behind the convention you follow?

+4  A: 

The rational behind the convention MyClass *ptr is that the * belongs to a single variable, so if you have:

MyClass *ptr1, ptr2

This declares ptr2 as a MyClass, not a MyClass*.

Barring the use of this horribly confusing type of declaration, it is a matter of style.

Justin Ardini