Quick question about objects

Started by
8 comments, last by Chocoboko 19 years, 8 months ago
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.
Advertisement
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".
- A momentary maniac with casual delusions.
Thank you. That explains a lot. I have another quick question. How does this differ from pointers? Thanks.
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.
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 membersObject a;Object* ptr = &aptr->someMethod();//with references, you can pretend they are just standard variables from a coding point of viewObject& ref = a;ref.someMethod();

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

HTH
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.
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement