C++ Style Question
-
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