I'm trying to learn the basics of C++. The book I'm reading uses the following syntax:
func(int& x);
On the internet, I mostly see the following syntax:
func(int &x);
Both seem to do exactly the same. Is there any difference?
I'm trying to learn the basics of C++. The book I'm reading uses the following syntax:
func(int& x);
On the internet, I mostly see the following syntax:
func(int &x);
Both seem to do exactly the same. Is there any difference?
Literally no difference. Just a stylistic preference. The same is true of where you put the pointer.
See the answer to the Is int* p;
right or is int *p;
right? FAQ on Bjarne Stroustrup's website. It's for pointers, but what he writes equally holds for references:
Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. [...] The choice between
int* p;
andint *p;
is not about right and wrong, but about style and emphasis.
I prefer int& x
. This way the reference becomes part of the type, it's an "int reference" called x, instead of "int, reference of x"
I find that thinking of "int reference" as a discrete type makes it less confusing, but to the compiler it's the same thing.