Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between int& a and int &a?

Tags:

c++

reference

This code:

int a = 5;
int& b = a;
b = 7;
cout << a;

prints out 7, and replacing int& b with int &b also prints out 7.

In fact so does int&b and int & b.

I tested this kind of behavior with a simple class as well. In general, does it ever matter whether the ampersand is placed relative to the type and identifier? Thanks.

like image 824
newprogrammer Avatar asked Dec 30 '11 02:12

newprogrammer


1 Answers

No, there is absolutely no difference except coding style. I think the main argument about coding style is that this declaration:

int& a, b;

declares a as an int& and b as an int.

like image 104
Ry- Avatar answered Sep 30 '22 19:09

Ry-