References & Pointers
description
Transcript of References & Pointers
CS-1030Dr. Mark L. Hornick
1
References & Pointers
CS-1030Dr. 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
CS-1030Dr. 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…
CS-1030Dr. 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
CS-1030Dr. 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)
CS-1030Dr. 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
CS-1030Dr. 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 datatypeint x = 3; // int x is assigned a value of 3
int* px = 0; // int* px is assigned to point // to memory address 0
CS-1030Dr. 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!!!
CS-1030Dr. 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
CS-1030Dr. 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
CS-1030Dr. 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 0x1234pValue++; // new addr is 0x1238; why not 0x1235????
-- prefix or postfix – decrement the address stored in the pointer
CS-1030Dr. 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
CS-1030Dr. Mark L. Hornick
13
Ran out of time here
CS-1030Dr. 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
CS-1030Dr. Mark L. Hornick
15
What about objects?
Dereferencing object pointers
SearchResult sr1; // an objectSearchResult* psr = &sr1; // ptr to sr1
SearchResults sr2 = *psr; // same as sr2=sr1
CS-1030Dr. 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
CS-1030Dr. Mark L. Hornick
17
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’d
pValue = &anotherValue // points to anotherValue
// whatever pValue points to now equals aValue: *pValue = aValue;// same as anotherValue=aValue;
CS-1030Dr. Mark L. Hornick
18
More Confusing syntax
int aValue = 1; // an intint anotherValue = 2;
int *p1 = &aValue; // ptr to aValueint *p2 = &anotherValue;
*p1 = *p2; // same as aValue=anotherValue
p1 = p2; // now both point to anotherValue
CS-1030Dr. 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
CS-1030Dr. 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)
CS-1030Dr. 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, whereint *pValue = NULL; // points to address 0
This is a convention that allows you to check to see if a pointer is valid