Difference between reference sign after data type or before variable name?

I was wondering if there is a difference when referring to a variable, where does the "link sign" go?

eg...

vector<int>& v;
vector<int> &v;

Is there a difference at all or is it just a preference?

+4
source share
3 answers

This is completely preference, but I like to keep it near the type when I declare a variable or specify a parameter. Thus, when I refer to a variable and use &my_variable, I just know that it is a reference to a variable and has nothing to do with the declaration. I do the same with pointers. I declare them int* ptr;, so when I see *ptr, I know that it just dereferences him.

+3
source

, , .

+1

You can also use:

vector<int>&v;
vector<int> & v;

no problem. From the point of view of the compiler, there can be no space or any number of spaces between vector<int>and &, and between &and v.

Where you use spaces and how many spaces you use is completely up to your coding standards.

0
source

All Articles