C++ Style Question
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
I prefer the first one's aesthetics - but it can hide the way C type-decls are actually decoded, and as Nish said, the multiple item declaration thing is an issue. The other alternative, of course, is to encapsulate the pointer-ness in a typedef:
typedef CType* CTypePtr;
CTypePtr Pointer = NULL;Helps especially with function pointers - which is easier to understand:
int (*myFn)(double, int);
or
typedef int (*MyFnPtr)(double, int);
MyFnPtr myFn;
? I guess some amount of the SPARK Ada development I've done (SPARK doesn't allow anonymous type defs) has rubbed off on me. Who'da thunk it...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I prefer the first because it groups the description of the type and the variable name separately.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Alternative 1, with a slight twitch
CType* Pointer(0);
rgds *I don't do multiple variable declarations*/Jonas
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Depends on where you want the emphasis to be. Personally, I prefer the
CType* Pointer
It reads more clearly that the variable Pointer is of "pointer type." It does mean though that it is difficult to declare multiple variables on a single line, but frankly, I think it is better style to only declare a single variable per line. The typedef mentioned in a previous post is very neat. I've used this a few times, it's even better at showing the "pointer type." If you can, prefer this.
-
I've been burned by multiple declarations enough that I always put the * on the variable name for clarity.
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies
Likewise.
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Personally I prefer the first option:
int* a
It doesn't look like I'm trying to dereference an existing variable that way and I think it makes it easier to read. Also I avoid writing multiple definitions on the same line, again it makes it easier to read in my opinion and it means that I don't have to change my coding style for pointer declaration. -
Wrong! It's a modifier for the variable like const. which is why you put it with the variable, not the data type.
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
C/C++ was my first introduction to programming years after some dabbling in Sinclair Basic. This was before Google, and even before ready access, for me, to forums etc. and for a long time, I thought there was a difference.
You really gotta try harder to keep up with everyone that's not on the short bus with you. - John Simmons / outlaw programmer.
-
i always use the 2nd, out of habit. the first would be clearer except you can do this:
CType* ptr, nonPtr;
but the * doesn't apply to all items in the var list. therefore it's not really part of the type, it's really tied to the individual variable. but that's not why i don't use it.
I couldn't have explained it better. I like to consider the asterisk as a modifier to the type. It does actually belong neither to the type, nor to the variable. Its name is ptr and not *ptr. Another example is:
int i = 123;
int &j = i;It becomes even more unclear if you consider &j as the variable name. Eventually, an alternate way to consider it is with parenthesis:
CType (*ptr), nonPtr;
-
You've directly hit upon my dilemma. The multiple declaration exposes the fact that the asterisk is an attribute of the variable, not the type proper. However, in a single declaration, it somehow makes a lot of sense to indicate that you're declaring a type-pointer. :confused:
int* p1,* p2;
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
The second, since the '
*
' associates with the variable name, not the type.Software Zen:
delete this;
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
-
Ditto. The second one always looks like a dereference to me - which it isn't.
Anna :rose: Having a bad bug day? Tech Blog | Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Just use c# instead
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Since I mostly work with existing code, I try to stay with the style already in the existing code; unless there is something really bad or goofy. I would follow what others have already done in the existing code. Most of the work I do has informal standards, where you only put one variable declaration on a single line. By practice (meaning there is no standard) we out the * on the variable:
int *some = NULL;
int *other = NULL; -
I prefer the first. Same with references:
Object& obj = someFunc();
as opposed to:
Object &obj = someFunc();
-
If this is really a programming question, I'll move it, but I don't consider it such. I'm trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name?
CType* Pointer = NULL;
vs.
CType *Pointer = NULL;
Its all up to your personal preference, the only time it matters is when you declare many variables in 1 line e.g
int* first, second
here both variables are type int*int *first, second
here first is int* and second is primitive int Personally i useint* i
the reason being that the data type is more clear when reading the code