90 likes | 100 Views
Overview of References and Pointers. Often need to refer to another object Without making a copy of the object itself Also known as “aliasing” Two ways to do this Directly, via a reference Acts as an alias for the object User interacts with reference as if it were the object itself
E N D
Overview of References and Pointers • Often need to refer to another object • Without making a copy of the object itself • Also known as “aliasing” • Two ways to do this • Directly, via a reference • Acts as an alias for the object • User interacts with reference as if it were the object itself • Indirectly, via a pointer • Gives the address (in memory) of the object • Requires the user to do extra work: dereferencing
A variable holding an address Of what it “refers to” in memory But with a nicer interface An alias to the object Hides indirection from programmer Must be typed Checked by compiler Again can only refer to the type to which it can point int &r = i; // can only refer to int Must always refer to something Must be initialized, cannot be changed More restricted than Java references What’s a Reference in C++? int i 7 0x7fffdad0 int &r
int main (int, char *[]) { const int i = 0; int j = 1; // non-const reference // r can’t refer to i int &r = j; // this is ok, though const int &s = i; const int &t = j; } Remember: references must refer to something Can’t be 0 (or NULL) Except through bad tricks like int *p = 0; int & r = *p; Also, once initialized, references cannot be changed E.g., can’t redirect t to i In Java, can re-assign references In C++, you cannot Const reference Promise not to change what’s aliased E.g., can’t use t to change j Can’t have a non-const reference alias a const variable Reverse is OK Const References
Write a simple main function or just use the one provided in the array_exercise0.cc file Declare two integers Give them different initial values Make one of them const Declare four references One const and one non-const reference initialized to alias each of the integers What happens when you compile? Comment out any reference declaration that’s illegal What is printed when you insert print the names and values of the variables and the (remaining) references into cout? Use a non-const reference to change a value What is printed when you for the same variables and references now? Do you understand why it prints what it prints each time? Exercise
A variable holding an address Of what it “points to” in memory Can be untyped void * v; // can point to any type However, usually they’re typed Checked by compiler Can only be assigned addresses of variables of the type to which it can point int * p; // can only point to int Addresses: garbage, something, nothing When created: int * p = i; vs. int * q; q = 0; // now it points to nothing p = NULL; // not portable, use 0 instead What’s a Pointer in C++? int k 7 const int j 3 int i 7 0x7fffdad0 int * p
Location and Value Comparisons • Pointers may be compared for equality • Same as comparing addresses of what pointers point to (memory locations: l-values) • Contents of what pointers point to may be compared, too (r-values) • First implies second, not other way around int *p 0xefffdad0 5 p == q &*p == &*q int i int *q 0xefffdbd0 5 *p == *q int j
int main (int, char *[]) { const int i = 0; int j = 1; int k = 2; // pointer to int int * w = &j; // const pointer to int int * const x = &k; // pointer to const int const int * y = &i; // const pointer to const int const int * const z = &j; } Make promises via the const keyword in pointer declaration: not to change pointer itself not to change value it aliases can also promise neither/both Read declarations right to left In this example, can change w and what it points to what x points to but not x y but not what it points to neither z nor what it points to A pointer to a non-const type cannot point to a const variable w and x can’t point to i Const Pointers and Pointers to Const Types
Write another simple main function Declare two integers Give them different initial values Make one of them const Declare four pointers, all initialized to 0 Make one of them a pointer to integer Make one of them a const pointer to integer Make one of them a pointer to const integer Make one of them a const pointer to const integer Try assigning addresses of integer variables Which pointers cannot change at all? Which pointer can point to either variable? Which pointer can only point to one of the variables? As you change the pointers Print out their names, addresses, and values of what they point to Exercise