Sign in to follow this  
Tree Penguin

Simple C question: type &variable ?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this