Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Screen* const &ScreenPtr ....(huh?)

This topic is 5149 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
It's a reference to a constant pointer to a Screen.
In other words, you can modify the screen, but not the pointer that's referenced.

- Pete

Share this post


Link to post
Share on other sites
A reference is a way to modify a variable using another name. Iternally, it is done with pointers. I'm sure by now that you've heard the phrase, "passing by reference". Well references provide the syntactic equivalent, sans the pointer dereferencing (since that is done automatically).


int main () {
int five = 5;
int& alias = five;
cout << five << endl << alias << endl; // outputs 5 5
five = 6;
cout << five << endl << alias << endl; // outputs 6 6
alias = 7;
cout << five << endl << alias << endl; // outputs 7 7
cout << (&alias == &five) << endl; // outputs true (they both have the same address in memory)
}





As you can see, this works a lot like a pointer, except it's "backed up a step". For all intents and purposes alias is the same variable as five. References must be initialized, and cannot have a null value. They are prefered to pointers in every case except when an initial value cannot be given or a null value is required (or you need to use pointer arithmetic).


void modify (int& arg) {
++arg;
}

int main () {
int five = 5;
cout << five << endl; // outputs 5
modify (five);
cout << five << endl; // outputs 6
}




Here the variable passed in acts as if it were the original variable.

The initialization requirement means that you must put members that are references into the initializer list...much like const members.

Well, I wish I could be more clear, but hopefully this helps you a little bit.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
The best analogy I've seen is...
a reference is a C++ vodoo doll. [wink]


I was this close to using that word in my post [wink].

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!