Session_7 - Lesson 9 References Think of a reference as an alias When you create a reference you initialize it with the name of another object the

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 9 - References Think of a reference as an alias. When you create a reference, you initialize it with the name of another object, the target. From that moment on, the reference acts as an alternative name for the target, and anything you do to the reference is really done to the target. You create a reference by writing the type of the target object, followed by the reference operator (&), followed by the name of the reference. References can use any legal variable name, but many programmers prefer to prefix all reference names with “r.” Thus, if you have an integer variable named someInt, you can make a reference to that variable by writing the following: ; Note that the reference operator is the same symbol as the one used for the “address of” operator. Example from our textbook: #include <iostream> using namespace std; int main() { int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; rSomeRef = 7; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; return 0; } Because rSomeRef is an alias for intOne, the 7 is really assigned to intOne as well. If you ask a reference for its address, it returns the address of its target, since references are aliases for their targets. Example below demonstrates this: #include <iostream> using namespace std; int main() { int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne: " << intOne << endl; cout << "rSomeRef: " << rSomeRef << endl; cout << "&intOne: " cout << "&rSomeRef: " return 0; } Page 1 of 13
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The addresses of the two variables are identical. C++ gives you no way to access the address of the reference itself because it is not meaningful. References are initialized when created, and they always act as a synonym for their target, even when the address of operator is applied. References Cannot be Reassigned References cannot be reassigned and are always aliases for their target. Good example below: #include <iostream> using namespace std; int main() { int intOne; int &rSomeRef = intOne; intOne = 5; cout << "intOne\t" << intOne << endl; cout << "rSomeRef:\t" << rSomeRef << endl; cout << "&intOne:\t" cout << "&rSomeRef:\t" int intTwo = 8; rSomeRef = intTwo; // LOOK AT THIS cout << "intOne\t" << intOne << endl; cout << "intTwo\t" << intTwo << endl; cout << "rSomeRef:\t" << rSomeRef << endl; cout << "&intOne:\t" cout << "&intTwo:\t" cout << "&rSomeRef:\t" return 0; } Since rSomeRef continues to act as an alias for intOne, so the assignment really is intOne = intTwo. Null Pointers and Null References
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/12/2011 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 13

Session_7 - Lesson 9 References Think of a reference as an alias When you create a reference you initialize it with the name of another object the

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online