Sign in to follow this  
Chocoboko

Quick question about objects

Recommended Posts

Hi, I have a quick question about objects. I know that when dealing with passing whole objects through a parameter of a function, some put a & by it. What is that supposed to do? Let me illustrate more. What's the difference between: int function(OBJECT &object); and int function(OBJECT object); Thank you.

Share this post


Link to post
Share on other sites
With the &, the argument is being passed by reference. Without it, the paramater is being passed by value.

Pass by Value: Make a temporary copy of the value being passed in for the function to work with.

Pass by Reference: Work with the value from the scope that called this function.


#include <iostream>
void f1(int x)
{
cout << x;
cout << ++x;
}

void f2(int& x)
{
cout << x;
++x;
cout << x;
}

int main()
{
int a = 1;

std::cout << a << std::endl;
f1(a);
std::cout << a << std::endl;
f2(a);
std::cout << a << std::endl;

return 0;
}




When f1(a) is called, f1 creates a copy as x.
When f2(a) is called, f2 works with the a in main.

After calling f2, the value of a in main will be changed as f2 worked with the value "by reference".

Share this post


Link to post
Share on other sites
Quote:
Original post by Chocoboko
Thank you. That explains a lot. I have another quick question. How does this differ from pointers? Thanks.


pointers are variables aswell so it's still pass by value except your making a copy of a address passed and not a copy of the instance. Where as reference is the actual object passed to the function no copy occurs, you should prefer using references for pass by reference over pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chocoboko
Thank you. That explains a lot. I have another quick question. How does this differ from pointers? Thanks.

References are constant, so you can't change what they are pointing to, for example:

Object a;
Object b;
Object& ref = a;

ref = b; //Can't be done

Also, for that same reason, references must be assigned values at their declaration.

Object& ref; //not allowed

//must be
Object& ref = someObject;


Another difference between pointers and references is the way you access the data they refer to:

//with pointers, you use '->' to dereference and access members
Object a;
Object* ptr = &a;

ptr->someMethod();

//with references, you can pretend they are just standard variables from a coding point of view
Object& ref = a;
ref.someMethod();

Those would be the main differences, there are probably more I've forgotten.

HTH

Share this post


Link to post
Share on other sites
Quote:
Original post by henrym
References are constant, so you can't change what they are pointing to, for example:

Object a;
Object b;
Object& ref = a;

ref = b; //Can't be done



You can do that but it doesn't change what reference refers to, it changes the original instance.

Share this post


Link to post
Share on other sites
The biggest difference between references and pointers is semantic. References are really pointers*, but they pretend they aren't. The reason they're in the language in the first place, in fact, is as a utility to operator overloading (see The Design and Evolution of C++ by Bjarne Stroustrup). They turn out to be a heck of a lot cleaner than pointers when you don't need all that wacky pointer arithmetic, though, so they tend to be used a lot in other circumstances. The general guideline is "use references when you can, and pointers when you have to". With that said, there is absolutely nothing that references can do that pointers can't.

* All right, so it's implementation-defined what a reference is. But everyone uses pointers to implement references.

Share this post


Link to post
Share on other sites
It is also the case that while pointers may be invalid or null, references are generally assumed not to be (you can produce invalid references, but this is widely seen as a problem in the code that produces the reference rather than an error in the code using it to take invalid references into account), so you don't need to perform null checks.

Share this post


Link to post
Share on other sites
Quote:
Original post by henrym
Also, for that same reason, references must be assigned values at their declaration.

Object& ref; //not allowed

//must be
Object& ref = someObject;



You can declare a reference without definition aswell e.g.


extern Object& ref;


But you must provide definition later on.

Share this post


Link to post
Share on other sites
Thank you. This helps a lot. I think I am gonna rewrite some code in my game to use references. It sounds very useful. Again, thank you.

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