when I declare C++ variables, I do it like this:
int a,b,c,d;
or
string strA,strB,strC,strD;
I.e., first the type then a comma separated list of variable names.
However, I declare pointers like this:
int *a,*b,*c,*d;
and references like this:
int &a,&b,&c,&d;
To be consistent it should be
int* a,b,c,d;
and
int& a,b,c,d;
Why is it not consistent?
It’s because of the C heritage. The
*modifier applies to the variable in C. So the C++ designers made&to apply to the variable as well by analogy, since they couldn’t change the first without breaking C compatibility. Same is true for the array syntax too:In The Design and Evolution of C++ Bjarne Stroustrup says he wasn’t happy with this but had to accept it for C compatibility. He was unhappy with this in particular:
It’s not clear from the declaration if
ais a pointer to an array or an array of pointers (it’s an array of pointers, you need brackets to override).Of course, you can always use a
typedefto clean things up a little.