Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/9

Click to flip

9 Cards in this Set

  • Front
  • Back
What is a reference?
An alternate name for an object
What are references typically used for?
Pass-by-reference, allowing a function to change value of passed-in parameter.
Show example of pass-by-reference and explain
void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
}

int main()
{
int x, y;
x = 5; y=10;
swap(x,y);
}

- Value changed in function affects value of variable outside function
Is a reference just a pointer to an object?
No. A reference IS the actual object, since you can change the object by changing the reference. It may, however, be implemented using a machine address to point to the object.
What happens if you assign a value to a reference?
You change the value of what is being referred to
What happens if you return a reference?
The function call can appear on the left-hand side of an assignment.
Show an example of returning a reference
class Array {
public:
float& operator[] (int index);
}

int main()
{
Array a;
a[i] = 9;
}
What does object.method1().method2() mean?
Method calls are chained
- object.method1() called, returns reference to object
- method2() of object returned is called
What is method chaining?
object1.method1().method2()
- method1 of object1 called
- method1 returns some object
- method2 of that new object called