Session_7 - Lesson 9 References Think of a reference as an...

Info icon This 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: int &rSomeRef = someInt ; 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. Using the Address of Operator & on References 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: " << &intOne << endl; cout << "&rSomeRef: " << &rSomeRef << endl; return 0; } Page 1 of 14
Image of page 1

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

View Full Document Right 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" << &intOne << endl; cout << "&rSomeRef:\t" << &rSomeRef << endl; 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" << &intOne << endl; cout << "&intTwo:\t" << &intTwo << endl; cout << "&rSomeRef:\t" << &rSomeRef << endl; return 0; } Since rSomeRef continues to act as an alias for intOne, so the assignment really is intOne = intTwo. Null Pointers and Null References When pointers are not initialized or when they are deleted, they ought to be assigned to null (0). This is not true for references. In fact, a reference cannot be null, and a program with a reference to a null object is considered an invalid program. When a program is invalid, just about anything can happen.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern