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;
What if you wanted to declare both a non-pointer and a pointer to CType on the same line? [Not something I favor, but nevertheless legal]. In other words, take the two lines
CType* v1;
CType v2;and combine them into one line. If you wrote the one line like this:
CType* v1, v2; // only v1 is pointer
I think it would be natural (and also incorrect) to think that both v1 and v2 were pointers to CType, whereas if you always associate the dereference operator with the variable, this confusion may be minimized:
CType *v1, v2; // only v1 is pointer
Best wishes, Hans
-
For single variable declarations I'd do :
int* p1;
int* p2;But for multiple variables this may result in confusion, for example :
int* p1, p2;
There, p1 is an int* but p2 is actually an int. So in such cases I'd write :
int *p1, *p2;
Of course as far as possible I would avoid multiple variable declarations. That's just not my style.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
I use the former style, mostly because I learned C++ from the Stroustrup's book, and that's what he does. BTW, there is another style you didn't mention:
CType * Pointer = NULL;
I call this style the “MindChanger”.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
CType * pointer = NULL;
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
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 second declaration – asterisk next to the variable name for both primitives and objects. The reason is that I very much like the pointers and I don’t want to insult them giving their asterisk to the type. :)
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
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 because it expresses the intent more clearly, but it's "incorrect" for the reason mentioned by others. It's because of this that I never declare multiple variables in one statement. Another thing I'll need to discuss with dmr when I get my time machine working... :~
-
For single variable declarations I'd do :
int* p1;
int* p2;But for multiple variables this may result in confusion, for example :
int* p1, p2;
There, p1 is an int* but p2 is actually an int. So in such cases I'd write :
int *p1, *p2;
Of course as far as possible I would avoid multiple variable declarations. That's just not my style.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkYou'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:
-
For single variable declarations I'd do :
int* p1;
int* p2;But for multiple variables this may result in confusion, for example :
int* p1, p2;
There, p1 is an int* but p2 is actually an int. So in such cases I'd write :
int *p1, *p2;
Of course as far as possible I would avoid multiple variable declarations. That's just not my style.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkNishant Sivakumar wrote:
avoid multiple variable declarations
Dr. Plum and I agree. :)
BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere
-
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;
What I was taught in my first C++ class is:
CType* Pointer = NULL; // proper style for C++
CType *Pointer = NULL; // proper style for C
Although this may be the instructor's opinion. It's what I've been doing ever since, however.
BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere
-
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;
First for same reason as Joe Woodbury.
Kevin
-
I prefer the first. Same with references:
Object& obj = someFunc();
as opposed to:
Object &obj = someFunc();
Ditto.
Kevin
-
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;
You can solve the problem by dropping the space. More seriously, putting more than one variable in the declaration (one pointer, one not) should show the way to do it. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
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
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"
-
For single variable declarations I'd do :
int* p1;
int* p2;But for multiple variables this may result in confusion, for example :
int* p1, p2;
There, p1 is an int* but p2 is actually an int. So in such cases I'd write :
int *p1, *p2;
Of course as far as possible I would avoid multiple variable declarations. That's just not my style.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkI avoid multiple pointer declarations on the same line as I always initialise them (never leave them dangling):
int* p1 = NULL;
int* p2 = NULL;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;
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.