Jump to content
  • Advertisement
Sign in to follow this  
Ancient Spirit

Pointers and References...

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 i
std::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 i
std::cout << i << std::endl; // outputs 6




BTW, is "dereference" the right term for what the * operator does? In german it's called "dereferenzieren".

Share this post


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

#include

class LargeClass {};

void RecieveLargeClass( const LargeClass & lc ) {
int i = lc.Accessor();
std::cout Mutator();
int i = lc->Accessor();
std::cout

Share this post


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

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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!