Tags: lec8

Q: If you're overloading operator<<, what should it take as arguments, and what should it return?
A: Arguments: a reference to a std::ostream and the object to be printed
Return: a reference to the same std::ostream that was passed in

Q: What is the most well-known example of a std::ostream?
A: std::cout

Q: What name is used for a method to indicate that it is a constructor for a class C?
A: ~C

Q: At the end of a C object's lifetime, what cleanup (if any) happens for "other" automatically?
class C {
    OtherClass other;
    ....
};
A: The bytes used to represent the "other" object are part of the C object's memory, so the memory for "other" is released together with the broader object.  Just before this happens, C++ will also call the destructor on "other" automatically (this is true whether or not you write a destructor for C).

Q: At the end of a C object's lifetime, what cleanup (if any) happens for "other" automatically?
class C {
    OtherClass* other;
    ....
};
A: "other" is a pointer, so it contains the address of an OtherClass object on the heap.  C++ has no way of knowing whether this is the ONLY active pointer to the heap object.  Thus, C++ will NOT release the memory for *other, and it will NOT call the destructor on other.
If you (the programmer) know that other is the last pointer to the object on the heap, it is your responsibility to call "delete other" in the destructor of C.  The "delete" command will trigger cleanup of the heap object (calling the OtherClass destructor, if any, and releasing that memory).

Q: If you have a class MyClass, C++ will usually create a default copy constructor for you, so that this is possible:
MyClass obj1 = ...;
MyClass obj2 = obj1;
What argument(s) does the copy constructor take?
A: A reference (perhaps with const) to a MyClass object.

Q: If you don't want C++ to create a copy constructor for you automatically, what can you do?
A: Write the declaration for the copy constructor, followed by " = delete;" in the class.

Q: If you're writing a destructor, why do you probably need to write a copy constructor too (or at least delete the default one)?
A: If you're writing a destructor, your class probably refers to other resources that need manual cleanup.
You generally don't want to delete a resource if there are still active references to it.
A default copy constructor will generally create new references to a resource for you, but without giving you a way to keep track of these additional references.  So you'll need to either suppress this behavior (uncopyable object), do some bookkeeping so you can track how many references are active (e.g., reference counting), or copy the references object so that we don't have multiple parents referring to the same child.

Q: If you're writing code for a copy assignment operator, what other special functions should you probably look at for inspiration?
A: (1) copy constructor and (2) destructor
You could look at the copy constructor to see what work is necessary for the copy.
Copy assignment is trickier, though, because the target object is not a fresh/empty object -- it already refers to some resources to which it will no longer refer.  The destructor is a good place to see what must be done when your object stops referring to these other resources.