Differences between int& foo and int &foo

Is there any difference between
1
2
3
int & foo; // 1
int& foo; // 2
int &foo; //3 


????
Technically, no, but IMO, the third is more readable.
I'm not sure you can do something like that anyway. I have never tried it, only ever done int *foo; which is a pointer, but you declared a reference...
closed account (Dy7SLyTq)
yes its possible. i like the third to because 1 makes it look more like a binary operator doing a calculation, 2 makes it look like foo is of type int& (well it technically is but i mean it looks like everything is of that type)
Yes, this should be pointed out (although it refers to pointers). For example

1
2
 
int* a, b, c; 


may trick you into thinking that all 3 variables are pointers to int. Using the third style, it's clear that only a is a pointer var.

1
2
 
int *a, b, c; 
closed account (Dy7SLyTq)
which for a while i was under the impression int* p1, p2, p3 meant all were pointers but that was because the guy who wrote the tutorial thought that to
I prefer option 2. int& foo.

The reference is part of the type. So it belongs with the type. The fact that you can't do int& foo, bar and have it work as you'd expect is an error in the language as far as I'm concerned. But that doesn't matter that much because you shouldn't declare things like that very often (or at all?) anyway.
Last edited on
closed account (Dy7SLyTq)
while i agree that int& should mean all of the variables on that line, in c++ & is only the part of the type for the first one
Alright good, I was a bit confused by the different placements I've seen; thinking they meant different things. Thanks for the clearing that up!

P.S. 100 posts, here I come.
Last edited on
Topic archived. No new replies allowed.