Sign in to follow this  

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

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

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

This topic is 4865 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this