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

Started by
3 comments, last by Thunder_Hawk 19 years, 8 months ago
This thread shows an interesting syntax that I haven't seen before. Now it's not exactly a beginner's topic, but I'm a beginner when it's comes to that. So could anyone tell me what he's trying to do. I would ask at in that thread but I don't want to hijack it.

Beginner in Game Development?  Read here. And read here.

 

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
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.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
The best analogy I've seen is...
a reference is a C++ vodoo doll. [wink]
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].
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement