Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-1030 Dr. Mark L. Hornick 1 References & Pointers.

Similar presentations


Presentation on theme: "CS-1030 Dr. Mark L. Hornick 1 References & Pointers."— Presentation transcript:

1 CS-1030 Dr. Mark L. Hornick 1 References & Pointers

2 CS-1030 Dr. Mark L. Hornick 2 Parameter passing in Java and C++ Both Java and C++ pass primitives by value Meaning that a copy of the primitive is passed into a method If the value of the primitive is changed in the method, only the copy of the primitive is changed Java always passes objects by reference C++, however… By default, objects are passed by value Meaning that a copy of an object is passed into a method

3 CS-1030 Dr. Mark L. Hornick 3 Pass by value: a good thing? Passing entire objects by value in C++ is not always desireable Objects can be big, leading to inefficiency Copy constructor must be called to make the copy of the object… If a method is supposed to modify the object, a copy of the object must be returned… Copy constructor must be called again to make a copy of the modified object…

4 CS-1030 Dr. Mark L. Hornick 4 Another way: References Passing entire objects by value in C++ is not always desireable Objects can be big, leading to inefficiency References were created to make life easier C++ references are similar to Java references

5 CS-1030 Dr. Mark L. Hornick 5 References – special syntax Review: Here’s a declaration of a method that passes an object by value: void printName(Employee e); Usage: printName( anEmp ); Here’s the modified declaration of the method to tell it to pass the object by reference instead: void printName(Employee& e); Usage: printName( anEmp ); // same as above References are only 4 bytes long (8 in Windows Vista)

6 CS-1030 Dr. Mark L. Hornick 6 Passing primitives by reference Review: Here’s a declaration of a method that passes a primitive by value: void setValue( int val ); Here’s the modified declaration of the method to tell it to pass a primitive by reference instead: void setValue( int& val ); Could be useful if you want setValue() to modify the actual val variable you are passing into the method

7 CS-1030 Dr. Mark L. Hornick 7 But there’s yet another way C++ has another kind of datatype called a pointer Pointers are used to store the address (of an item) An address is legitimate data by itself Every memory location has an address Java has no capability for this Example of a pointer datatype Review: int is a datatype int* (“pointer to int”) is a separate datatype int x = 3;// int x is assigned a value of 3 int* px = 0; // int* px is assigned to point // to memory address 0

8 CS-1030 Dr. Mark L. Hornick 8 Pointer syntax Declaration of a pointer uses the * prefix int *pValue; // pointer to an integer int* pValue; // another way of declaring the same Be careful with multiple declarations on one line: int *pValue1, *pValue2; int* pValue1, pValue2; // No!!!

9 CS-1030 Dr. Mark L. Hornick 9 How can you assign a pointer? int x = 3;// int x is assigned a value of 3 int* px = &x; // int* px is assigned to point // to memory address where x is New unary operator: & prefix – “address of” operator

10 CS-1030 Dr. Mark L. Hornick 10 How do you get the value of what’s at the location where the pointer is pointing? int x = 3;// int x is assigned a value of 3 int* px = &x; // int* px is assigned to point // to memory address where x is int y;// another int variable y = *px;// y is assigned the value at the // memory location px points to Another new unary operator: * prefix – dereference operator

11 CS-1030 Dr. Mark L. Hornick 11 More unary operators ++ prefix or postfix – increment the address stored in the pointer int aValue = 3; int* pValue = &aValue; // addr might be 0x1234 pValue++; // new addr is 0x1238; why not 0x1235???? -- prefix or postfix – decrement the address stored in the pointer

12 CS-1030 Dr. Mark L. Hornick 12 What about objects? for any “regular” datatype, you can also declare a corresponding datatype: SearchResult sr1; // an object SearchResult sr2; // another object SearchResult* psr = &sr1; // ptr to sr1 psr = &sr2; // now points to sr2

13 CS-1030 Dr. Mark L. Hornick 13 Ran out of time here

14 CS-1030 Dr. Mark L. Hornick 14 How do you get the value of what’s at the location where the pointer is pointing? int x = 3;// int x is assigned a value of 3 int* px = &x; // int* px is assigned to point // to memory address where x is int y;// another int variable y = *px;// y is assigned the value at the // memory location px points to Another new unary operator: * prefix – dereference operator

15 CS-1030 Dr. Mark L. Hornick 15 What about objects? Dereferencing object pointers SearchResult sr1; // an object SearchResult* psr = &sr1; // ptr to sr1 SearchResults sr2 = *psr; // same as sr2=sr1

16 CS-1030 Dr. Mark L. Hornick 16 Passing items by address Review: Here’s a declaration of a method that passes a primitive by value: void setValue( int val ); Here’s the modified declaration of the method to tell it to pass a primitive by address instead: void setValue( int* val ); Usage: setValue( &x ); // must pass address of x

17 CS-1030 Dr. Mark L. Hornick 17 Confusing syntax int aValue = 1; // an int int anotherValue = 2; int* pValue = &aValue; // ptr to aValue // above line is same as: // int* pValue; // ptr, but not init’d // pValue = &aValue; // now init’d pValue = &anotherValue // points to anotherValue // whatever pValue points to now equals aValue: *pValue = aValue;// same as anotherValue=aValue;

18 CS-1030 Dr. Mark L. Hornick 18 More Confusing syntax int aValue = 1; // an int int anotherValue = 2; int *p1 = &aValue; // ptr to aValue int *p2 = &anotherValue; *p1 = *p2; // same as aValue=anotherValue p1 = p2; // now both point to anotherValue

19 CS-1030 Dr. Mark L. Hornick 19 The this keyword in C++ Sometimes an object must refer to itself Java and C++ both use the this keyword In Java, this is a reference to the current object In C++ this – a pointer to the current object *this – a dereferenced pointer = the current object Accessible in member functions Usage is implied in member functions

20 CS-1030 Dr. Mark L. Hornick 20 Method access via pointers Employee anEmp; // an object Employee* pe = &anEmp; // ptr to object int id; id = anEmp.getID(); // method access id = pe->getID(); // via pointer Another new unary operator: -> the indirection operator (called a bigraph)

21 CS-1030 Dr. Mark L. Hornick 21 The NULL pointer Since a pointer holds an address, the pointer can be made to point anywhere in memory. int *pValue; pValue = 0x1234; // points to whatever is there… Pointers should only be made to point at valid addresses The exception is the NULL pointer, where int *pValue = NULL; // points to address 0 This is a convention that allows you to check to see if a pointer is valid


Download ppt "CS-1030 Dr. Mark L. Hornick 1 References & Pointers."

Similar presentations


Ads by Google