Quote:Which style do you prefer foo1 foo2 foo3?
#include main() { double bar = 300; double* foo1 = &bar // - [1] double * foo2 = &bar // - [2] double *foo3 = &bar // - [3] std::cout << "foo1 = " << *foo1 << std::endl << "foo2 = " << *foo2 << std::endl << "foo3 = " << *foo3; } // output //foo1 = 300 //foo2 = 300 //foo3 = 300
🔒 Locked
c++ pointer initialization.
Advertisement
Original Post
foo3
3 all the way!
Firslty if you want to declare multiple pointers on the one line its:
double *p1, *p3, *p3;
The idea being you have a double, and you create 3 points to that type. "Double Pointer" isn't the type, the type is "Double" and you create a pointer to it. Hence you want the '*' next to the vars name. Same with de-referencing something ... you put the '*' right next to the variable. In my mind its inconsistant to create a pointer with double* name; then de-ref it with the *name ... the * should always be attached.
But its 100% a style thing and makes no difference to code :D Its not something I'd ever actually worry about as it doesn't make your code easier/harder to read. Now if you want real style wars lets talk about whether or not you put '{' on a new line ^_^ that is the mother of all code formatting arguments hehe
Firslty if you want to declare multiple pointers on the one line its:
double *p1, *p3, *p3;
The idea being you have a double, and you create 3 points to that type. "Double Pointer" isn't the type, the type is "Double" and you create a pointer to it. Hence you want the '*' next to the vars name. Same with de-referencing something ... you put the '*' right next to the variable. In my mind its inconsistant to create a pointer with double* name; then de-ref it with the *name ... the * should always be attached.
But its 100% a style thing and makes no difference to code :D Its not something I'd ever actually worry about as it doesn't make your code easier/harder to read. Now if you want real style wars lets talk about whether or not you put '{' on a new line ^_^ that is the mother of all code formatting arguments hehe
i always use 1 as well (for pointer and reference declarations) to help me distinguish between a double, double*, and a double**. it always looked odd to me having a space after the double for pointers. but that's just my personal taste. for multiple pointers i usually do:
double* p1, *p2, *p3;
i know, not consistent, but tis my personal taste :-).
double* p1, *p2, *p3;
i know, not consistent, but tis my personal taste :-).
To spread Mr. Holy Stroustrup's words, "C++ put an emphasis on types, not variables". The prefered way to show this is to put the * (or &) near the type name. And don't declare two pointers/variables on the same line.
(now that I spread the words, I can say that I still prefer [3] [smile])
(now that I spread the words, I can say that I still prefer [3] [smile])
I stick with the foo3 style all the time. Why?
double* x, y, z;
This looks like it creates 3 pointers when in fact it only creates one. I know everyone already knows this, but to a beginner it's more than ambiguous. Then this:
double* x, *y, *z;
looks inconsistent to me. So I always stick with 3:
double *x, *y, *z;
double **x, **y, **z;
double &x, &y, z
double* x, y, z;
This looks like it creates 3 pointers when in fact it only creates one. I know everyone already knows this, but to a beginner it's more than ambiguous. Then this:
double* x, *y, *z;
looks inconsistent to me. So I always stick with 3:
double *x, *y, *z;
double **x, **y, **z;
double &x, &y, z
Quote:
Original post by t0Xic
I just wondered if anyone of you actually usesrather thanint const foo = 0;t0}{!cconst int foo = 0;
I do. I found a couple of reasons to always place const on the right (compound typedef types and templates) and no solid reasons to ever place it on the left, so I changed my personal coding style to match the above. It's not a huge point though. I'm still perfectly happy and able to write code either way.
Σnigma
Quote:
Original post by Enigma Quote:
Original post by t0Xic
I just wondered if anyone of you actually usesrather thanint const foo = 0;t0}{!cconst int foo = 0;
I do. I found a couple of reasons to always place const on the right (compound typedef types and templates) and no solid reasons to ever place it on the left, so I changed my personal coding style to match the above. It's not a huge point though. I'm still perfectly happy and able to write code either way.
Σnigma
Yeah, I agree. I still place consts on the left, but when you think about constant pointers it makes more sense:
int const * const x;
Looks better than:
const int * const x;
In the first example both the const statements affect the thing to the left.
Type 3 is what I prefer for already mentioned reasons.
The only thing I do not like about its look is in case you initiate a variable on declaration it looks like you are assigning an address to the address of the pointer instead of an address to the pointer:
" int *a = &b " looks like " *a = &b ".
[Edited by - kiome on August 23, 2006 9:03:18 PM]
The only thing I do not like about its look is in case you initiate a variable on declaration it looks like you are assigning an address to the address of the pointer instead of an address to the pointer:
" int *a = &b " looks like " *a = &b ".
[Edited by - kiome on August 23, 2006 9:03:18 PM]
A friend of mine used the following for a while. Lord willing, he has since stopped.
He didn't really have a choice as to where to place the spaces.
CM
#define PTR *#define REF &double PTR x;double REF y;He didn't really have a choice as to where to place the spaces.
CM
I prefer:
double* foo;
double& foo;
as opposed to:
double *foo;
double &foo
simply cause I see the * or & as part of the type. But its just a space, as a programmer u need to be able to look at either method and understand whats going on. So use what u like to use, but get used to other people's code so u can edit there's without needing to reformat everything.
double* foo;
double& foo;
as opposed to:
double *foo;
double &foo
simply cause I see the * or & as part of the type. But its just a space, as a programmer u need to be able to look at either method and understand whats going on. So use what u like to use, but get used to other people's code so u can edit there's without needing to reformat everything.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement