210 likes | 295 Views
References & Pointers. 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
E N D
References & Pointers CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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… CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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) CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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!!! CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
More unary operators • ++ prefix or postfix – increment the address stored in the pointer int aValue = 3;int* pValue = &aValue; // addr might be 0x1234pValue++; // new addr is 0x1238; why not 0x1235???? • -- prefix or postfix – decrement the address stored in the pointer CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
Ran out of time here CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
What about objects? • Dereferencing object pointers SearchResult sr1; // an objectSearchResult* psr = &sr1; // ptr to sr1 SearchResults sr2 = *psr; // same as sr2=sr1 CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
Confusing syntax int aValue = 1; // an intint anotherValue = 2; int* pValue = &aValue; // ptr to aValue// above line is same as:// int* pValue; // ptr, but not init’d// pValue = &aValue; // now init’dpValue = &anotherValue // points to anotherValue// whatever pValue points to now equals aValue: *pValue = aValue;// same as anotherValue=aValue; CS-1030 Dr. Mark L. Hornick
More Confusing syntax int aValue = 1; // an intint anotherValue = 2; int *p1 = &aValue; // ptr to aValueint *p2 = &anotherValue;*p1 = *p2; // same as aValue=anotherValuep1 = p2; // now both point to anotherValue CS-1030 Dr. Mark L. Hornick
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 CS-1030 Dr. Mark L. Hornick
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) CS-1030 Dr. Mark L. Hornick
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, whereint *pValue = NULL; // points to address 0 • This is a convention that allows you to check to see if a pointer is valid CS-1030 Dr. Mark L. Hornick