Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Reference Sign After Data Type or Before Variable Name?

Tags:

c++

I was wondering is there is a difference when referencing a variable, where the 'reference sign' goes?

such as...

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

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

like image 280
anacy Avatar asked Oct 03 '22 00:10

anacy


2 Answers

It's totally preference, but I like to keep the & next to the type when I'm declaring a variable or specifying a parameter. That way, when I'm referencing a variable and use &my_variable, I know just by looking at it that it's a variable reference and has nothing to do with a declaration. I do the same thing with pointers. I declare them int* ptr; so that way when I see *ptr I know it's just dereferencing it.

like image 69
Hugo Avatar answered Oct 13 '22 11:10

Hugo


Totally preference, it's the only order that's important.

like image 40
splrs Avatar answered Oct 13 '22 12:10

splrs