Pointers and References...

Started by
13 comments, last by GameDev.net 18 years, 7 months ago

int x, y;

int &ref = x;
int *ptr = &y;

Can you please tell when is it better to use pointers and when to use references? Thanks.
This world is ruled by big furry CATS!! :)Meow! :)
Advertisement
From the C++ FAQ lite:
Quote:
Use references when you can, and pointers when you have to.
References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.
Jooleem. Get addicted.
Generally speaking, it is best to use a reference when you don't wish to deal with the dereferencing of the pointer, and you don't need to change the address it is pointing to. For instance:

int &ref = somevariable;
ref++;

You don't need to go *ref++, it's more convenient, however, you cannot do:
ref = &someothervariable

If you ever wish to change the address that the variable is pointing to, you must use a standard pointer. I'm sure there are other slight differences involved, but I believe these are the major differences.
Quote:Original post by Peregrin
From the C++ FAQ lite:
Quote:
Use references when you can, and pointers when you have to.
References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.


what is "reseating"?

Sorry for my bad english...
This world is ruled by big furry CATS!! :)Meow! :)
See the link I gave, all is explained: Reseating a reference means making it refer to a different object.
Jooleem. Get addicted.
Quote:Original post by DMatovich
int &ref = somevariable;
ref++;

You don't need to go *ref++, it's more convenient

And more importantly, it would not be semantically equivalent!

int i = 5;int &x = i;x++; // increase istd::cout << i << std::endl; // outputs 6


int i = 5;int *x = &i;*x++; // dereference x, then increase x (!)std::cout << i << std::endl; // outputs 5


What you want is:

int i = 5;int *x = &i;(*x)++; // dereference x, then increase istd::cout << i << std::endl; // outputs 6


BTW, is "dereference" the right term for what the * operator does? In german it's called "dereferenzieren".
Quote:Original post by Fred304
BTW, is "dereference" the right term for what the * operator does?

Yes.

CM
For function arguments not of a basic type, prefer to pass a const reference when you can (by default), and an address when you need to:

#include

class LargeClass {};

void RecieveLargeClass( const LargeClass & lc ) {
int i = lc.Accessor();
std::cout Mutator();
int i = lc->Accessor();
std::cout
For function arguments not of a basic type, prefer to pass a const reference when you can (by default), and an address when you need to:
#include <iostream>class LargeClass {};void RecieveLargeClass( const LargeClass & lc ) {   int i = lc.Accessor();   std::cout << std::endl << i;   return;}// Since passing an address of a variable rather// than a its reference makes it clear the variable// may be modified, you may choose to do this:void RecieveLargeClass( LargeClass * const lc ) {   lc->Mutator();   int i = lc->Accessor();   std::cout << std::endl << i;   return;}// But again, a reference is preferred to avoid dangerous pointer mishaps.
Quote:Original post by Anonymous Poster
For function arguments not of a basic type, prefer to pass a const reference when you can (by default), and an address when you need to:
*** Source Snippet Removed ***

Also, passing an address allows you to deal with no-objects (zero pointer), so if you need to have that capability pass an address. However you must remember to check for the zero pointer before you dereference it, or your program will crash.

Have a nice day.

This topic is closed to new replies.

Advertisement