• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
C++ Pointers And References
 

C++ Pointers And References

on

  • 415 views

 

Statistics

Views

Total Views
415
Views on SlideShare
415
Embed Views
0

Actions

Likes
0
Downloads
8
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    C++ Pointers And References C++ Pointers And References Presentation Transcript

    • 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
    • Untangling Operator Syntax Symbol Used in a declaration Used in a definition unary & (ampersand) reference int i = 3; int &r = i; address-of p = &i; unary * (star) pointer int * p; dereference (get what’s pointed to) *p = 7; -> (arrow) member access via pointer cp->add(3); . (dot) member access (same syntax for either reference or object) c.add(3); [] (square bracket) array dimensions int a[3]; array indexing cout << a[0] << endl;
    • What’s a Reference in C++?
      • 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
      0x7fffdad0 7 int i int &r
    • Const References
      • 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
    • Exercise
      • 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?
    • What’s a Pointer in C++?
      • 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
      0x7fffdad0 7 int i int * p 3 const int j 7 int k
    • 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
      p == q &*p == &*q *p == *q 0xefffdad0 5 int i int *p 5 int j 0xefffdbd0 int *q
    • Const Pointers and Pointers to Const Types
      • 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
    • Exercise
      • 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