Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2004, 4:24 PM   #1
gamehack
Newbie
 
Join Date: Sep 2004
Posts: 12
Rep Power: 0 gamehack is on a distinguished road
Send a message via ICQ to gamehack
Hello all

I have been asking myself what are the essential differences between references and pointers. The only thing I could think of was that dynamic alocation is done with pointers. So, generally, when should I use pointers and references ? Can I replace every reference with the pointer equivalents and vice-versa ?

Thanks in advance

Cheers,
gamehack
gamehack is offline   Reply With Quote
Old Sep 19th, 2004, 4:34 PM   #2
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
References are used mainly for passing large structures to functions. When passing data to functions you have 2 options:
1. Pass by value
This passes a new instance of the variable to the function.
2. Pass by reference
This passes the address of the varaible to the function.

Passing by reference is more efficient as well as it still allows you to use the variables name with out having to worry about dereferencing it. References also allow you to change the data of the variable passed without having to return it. This can be a good thing and a bad thing so 'const' is often used in conjunction with references.

References are choosen above pointers for this reason since they are easier to use. Pointers are gernerally left for dynamically allocating memory and such and referencing dynamic objects.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005!
Ravilj is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC