Simple C question: type &variable ?

Started by
5 comments, last by iMalc 19 years, 2 months ago
What does the & sign mean in code like: vec2 & operator-=(const vec2 & u) { x-= u.x; y-= u.y; return *this; } Does it mean like, don't pass the variable itself but a copy of it or is it something else?
Advertisement
It means pass a reference to the variable, which is similar to a pointer to a variable, just different notation.

void func(int& a){  a = 5;}int main(){   int a;   func(a);   cout << a;}


Will print 5 to the console.

An advantage of using references is speed, instead of copying the entire structure it just passes the reference, this is useful if you have large classes.

However it can be a little insecure, because the values can be modified, so if you want speed with security make sure that you pass constant references or

void example(const int& in){   in = 4; // ERROR : you can't change the value of in   int b = in; // This is ok we can still access in, just not ovrewrite it}
It's the reverse of what you said. You can use the parameter-variable as the original variable. When the &-sign is left out, you would get a copy.
Ah ok, thanks.
You're on the right lines, although it actually means pretty much the opposite of what you said (it is to do with copying the value though).

Short explanation:
type &variable is the syntax for a reference. It's just like a pointer, except: 1) You don't have to dereference it, and 2) You can't change what it points at.

Long explanation:
There are three ways that values can be passed two and from functions:
By value - this means that the value of the variable gets copied. For example, if you have a function:
int foo(int a) { a += 5; return a; }
And call it with foo(b); Then it will return b + 5, but b will remain unchanged. The parameter is being passed in by value, so a copy of b (5) is made (and called 'a' in the function), then incremented by 5, then returned. But it was a copy that was changed, so 'b' will remain the same.

By pointer - this means that the address in memory of the variable is passed. For example:
int foo(int *a) { *a += 5; return *a; }
Called with foo(&b); It will return b + 5, and b will be changed. The parameter is passed by pointer. So, when you call it, you have to pass the address of 'b' (ie, '&b') not b itself, and when the function wants to increment it, it has to dereference the pointer ('*a') to get at the value. 'b' is changed, because no copy is made.

By reference - as in the short explanation. For example:
int foo(int &a) { a += 5; return a; }
Called with foo(b); It returns b + 5, and b is changed. But you don't have to worry about using &b, or *a, because it's all done implicitly by the compiler (pointers still have uses though, since you can also change the pointer itself not just the memory it points to, which is very useful, as you may know, or if not, you'll find out. Also, some people argue that pointers are better because it makes it more obvious in your code that you're modifying the original values and not copies. I'm not going to argue either way - make up your own mind. It's basically a decision between "safety" of not having to dereference things when you write the code, and the safety of the code being harder to mis-interpret when you read it again to change something or fix a bug)

So endeth the lesson, and if no one else has replied between when I started writing this and when I click 'Reply', then I'll be very surprised.

Edit: Well, only 3 people managed to reply before me. Not too bad [wink].

Hope that helps,

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
It's C++, not C
Quote:Original post by petewood
It's C++, not C
Ah yeah, good point. If references were in C then they would have made 'this' a reference rather than a pointer. Damn backwards compatability.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement