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;
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 -
i would follow the czar of c++. you might want to consider keeping one style/idiom for programming in c, and follow dr. stroustrup's examples for c++. kind regards,
David
-
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;
1. C and C++ have a basic style that says variables are declared in the same format in which they're used. Other examples are arrays and function pointers. To be consistent with that style, you would use "Ctype *foo", not "Ctype* foo" or "Ctype * foo". 2. The point about multiple decls is well made, e.g. "Ctype *foo, *bar". This illustrates the real nature of the "*" in the decl, which argues for "Ctype *foo" for a single decl. 3. IMHO what's more important is a naming convention for pointers. I maintain 1/2 million code lines of C with heavy pointer use, and I never have dereferencing bugs, because we use such a convention: "p_" prefix on names to indicate a pointer ("pp_" for double pointers, etc.), i.e. "p_foo" in the example above. (Another convention is a suffix of "P", i.e. "fooP" in the example.) Let's say you see "p_foo = *pp_bar;" in some code. Once your eyes learn to balance the levels using the convention, you can see that this is correct, whereas "p_foo = **pp_bar;" looks wrong. Stephen F. Heffner, President | Phone: +1(480)626-5503 ------------ | Fax: +1(480)626-7618 Pennington Systems Incorporated | Email: Heffner@Pennington.com 8655 East Via de Ventura, Suite G200 | Web: http://WWW.Pennington.com Scottsdale, Arizona 85258-3321 USA | XTRAN: A software engineering meta-tool