Tags: lec9

Q: What are some cases a vector would call a constructor for us for the objects it contains?
A: resize -- vector will call the default constuctor (the one with no args) to initialize the added elements.  This is in contrast to reserve, which does NOT initialize the new memory via constructor calls.
emplace_back -- this function takes arguments that it forwards to the constructor

Q: When would a vector call destructors for us?
A: When the lifetime of the vector ends (e.g., because it is on a stack frame being popped), the lifetime of the elements ends as well.  If those elements are values, the vector will destruct them for us.  If the elements are pointers to values, the vector will not delete the objects pointed to (or otherwise call the destructor on them) because there might be other active references to them in the program.

Q: If items is a vector, items[N] doesn't do bounds checking automatically.  How can you get bounds-checking behavior?
A: items.at(N)

Q: What is the emplace equivalent of this call where items is a vector?
items.push_back({x,y,z})
A: items.emplace_back(x,y,z);

Q: Which modifications (if any) through nums2 will be visible through nums1?
  vector<int> nums1{1,2,3};
  vector<int> nums2 = nums1;
  nums2[0] = 10; // modification 1
  nums2.push_back(4); // modification 2
  for (auto num : nums1)
    cout << num << "\n";
A: Neither!  "nums2 = nums1" creates a copy of the elements.

Q: How do you declare (a) an lvalue reference and (b) an rvalue reference of type MyClass?
A: - lvalue reference: MyClass& obj
- rvalue reference: MyClass&& obj

Q: What does std::move(obj) do?
A: Cast obj to an rvalue reference.  It doesn't actually move anything yet!

Q: If we have an rvalue reference to an object, what does that imply?
A: We can steal resources from the object because its useful life is ending.