Jump to content
  • Advertisement
Sign in to follow this  
Danikar

Pointers vs References

This topic is 4515 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

What is the diffrence between a *pointer and a &reference? And when is it appropriate to use one or another. I know both of them can be used to change a variable from another function without copying it. But then technicly arnt they the same thing? I know if u cout *pointer u get the value and if u cout pointer u get the memory address and if u cout $reference u get the memory adress and if u cout reference u get the value. So a reference to me looks like a reversed pointer. Anyone explain this to me better?

Share this post


Link to post
Share on other sites
Advertisement
here are some links:

http://www.gamedev.net/community/forums/topic.asp?topic_id=153872
http://www.gamedev.net/community/forums/topic.asp?topic_id=335936
http://www.gamedev.net/community/forums/topic.asp?topic_id=23691

Share this post


Link to post
Share on other sites
References specifically alias something. They're a higher-level, more specific construct, which don't cost you anything vs. pointers (except the ability to use them in certain ways, e.g. pointer arithmetic and casting) and can help the compiler (because you give it more information to work with). Generally you should use them where possible, and pointers where necessary.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need pointers for memory allocation, I think.

Share this post


Link to post
Share on other sites
A reference is automatically dereferenced for you and, thus, you cannot change what it is pointing to because it's yielding the final value, so it's like a constant pointer. On the other hand, with a pointer, you have to maunally dereference it to get and set a value and it also gives you the ability to change what it points to, unlike a reference.

- xeddiex

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!