Skip to main content
gamedev.net
🔒 Locked

c++ pointer initialization.

Started by pikapika 19 replies 2.8k views
Original Post
pikapika
pikapika
Quote:
#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
Which style do you prefer foo1 foo2 foo3?
ToohrVyk
ToohrVyk
I prefer none. I use double* ptr for personal history reasons, just for consistency.
PinguinDude
PinguinDude
foo3
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
kaysik
kaysik
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
yadango
yadango
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 :-).
pikapika
pikapika
thanks for the replies.

I feared i could be just dyslexic.

This is because to my eyes it looks like your not initializing but de-referencing.

Now I see the reason for using the asterisk just before the variable indentifier
is for multivariable declations. :)
dave
dave
I think 3 is the way it should be done, but i do 1.
Emmanuel Deloget
Emmanuel Deloget
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])
TheOddMan
TheOddMan
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
t0Xic
t0Xic
I just wondered if anyone of you actually uses
int const foo = 0;

rather than
const int foo = 0;


t0}{!c
Enigma
Enigma
Quote:
Original post by t0Xic
I just wondered if anyone of you actually uses
int const foo = 0;
rather than
const int foo = 0;
t0}{!c

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
TheOddMan
TheOddMan
Quote:
Original post by Enigma
Quote:
Original post by t0Xic
I just wondered if anyone of you actually uses
int const foo = 0;
rather than
const int foo = 0;
t0}{!c

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.
Anon Mike
Anon Mike
I'm a 2 guy. I never declare more than one variable on a line so the whole "double *a, *b" thing is irrelevant.
-Mike
kiome
kiome
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]
My Blog
Conner McCloud
Conner McCloud
A friend of mine used the following for a while. Lord willing, he has since stopped.
#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
yadango
yadango
ha ha ha since i study a lot of microsoft code, i usually kinda pick up on the windows-style habits ie. "typedef double* LPDOUBLE;" (this statement is kinda another reason i like #1 ).
Ryan_001
Ryan_001
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.