|
| 1 | +# Lecture 13 - Oct. 27, 2015 |
| 2 | + |
| 3 | +"The Big Three", Copy Constructor, Destructor, operator= |
| 4 | + |
| 5 | +## Copy Constructor |
| 6 | + |
| 7 | +Used to construct a new object as a copy of an existing object. |
| 8 | + |
| 9 | +```cpp |
| 10 | +Student billy(60, 70, 80); |
| 11 | +Student bobby = billy; |
| 12 | +``` |
| 13 | +
|
| 14 | +There is a built-in copy constructor. |
| 15 | +
|
| 16 | +Every class "comes with": |
| 17 | +
|
| 18 | +* Default constructor **(goes away when you write your own constructor)** |
| 19 | +* Copy constructor |
| 20 | +* Destructor |
| 21 | +* Copy assignment operator |
| 22 | +
|
| 23 | +For a class C, the copy constructor takes one parameter: const reference to object. |
| 24 | +
|
| 25 | +```cpp |
| 26 | +class C { |
| 27 | + public: |
| 28 | + C(const C &other) {}; //convention is to use other or rhs (right hand side) |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +An example of this is shown with the Student class: |
| 33 | + |
| 34 | +```cpp |
| 35 | +class Student { |
| 36 | + private: |
| 37 | + int assignments; |
| 38 | + int midterm; |
| 39 | + int final; |
| 40 | + public: |
| 41 | + Student(const Student &other) |
| 42 | + : assignments(other.assignment) |
| 43 | + , midterm(other.midterm) |
| 44 | + , final(other.final) {} |
| 45 | +} |
| 46 | +``` |
| 47 | +
|
| 48 | +The copy constructor shown above is the same behavior as the built-in copy constructor. |
| 49 | +
|
| 50 | +Consider the following: |
| 51 | +
|
| 52 | +```cpp |
| 53 | +class Node { |
| 54 | + private: |
| 55 | + int data; |
| 56 | + Node *next; |
| 57 | + public: |
| 58 | + Node(int data, Node *next) |
| 59 | + : data(data) |
| 60 | + , next(next) |
| 61 | + {} |
| 62 | + Node(const Node &other) |
| 63 | + : data(other.data) |
| 64 | + , next(other.next) |
| 65 | + {} |
| 66 | +}; |
| 67 | +
|
| 68 | +Node *np = new Node(1, new Node(2, newNode(3, nullptr))); |
| 69 | +Node m = *np; //copy constructor |
| 70 | +Node *npCopy = new Node(*np); //copy constructor |
| 71 | +
|
| 72 | +//68: np[-]--> [1|-]--> [2|-]--> [3|/] |
| 73 | +//69: m[1|-]-------------^ points to *np's next instead of doing a deep copy |
| 74 | +//70: npCopy[-]-->[1|-]--^ similarly, points to *np's next |
| 75 | +``` |
| 76 | + |
| 77 | +The default copy constructor does a shallow copy. |
| 78 | + |
| 79 | +We often want a "deep copy" when an object contains fields that are dynamically allocated (i.e. on the heap). |
| 80 | + |
| 81 | +What we should do is the following: |
| 82 | + |
| 83 | +```cpp |
| 84 | +class Node { |
| 85 | + private: |
| 86 | + int data; |
| 87 | + Node *next; |
| 88 | + public: |
| 89 | + Node(const Node &other) { |
| 90 | + this->data = other.data; |
| 91 | + if (other.next == nullptr) { |
| 92 | + this->next = nullptr; |
| 93 | + } else { |
| 94 | + // Need to dereference, we send in a Node object. |
| 95 | + this->next = new Node(*(other.next)); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | +
|
| 101 | +This will do a deep copy of the linked list. |
| 102 | +
|
| 103 | +We show an alternative way of writing the copy constructor: |
| 104 | +
|
| 105 | +```cpp |
| 106 | +class Node { |
| 107 | + private: |
| 108 | + int data; |
| 109 | + Node *next; |
| 110 | + public: |
| 111 | + Node(const Node &other) |
| 112 | + : data(other.data) |
| 113 | + , next(other.next ? new Node(*(other.next)) : nullptr) |
| 114 | + {} |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +Places where a copy constructor is called: |
| 119 | + |
| 120 | +1. Creating an object as a copy of another |
| 121 | +2. When an object is passed by value |
| 122 | +3. When an object is returned by value (returning a stack allocated object) |
| 123 | + |
| 124 | +We must pass in a const reference to a copy constructor. |
| 125 | + |
| 126 | +This is because if we pass in the object by value, the copy constructor will be invoked each time. |
| 127 | + |
| 128 | +### Single Parameter Constructors |
| 129 | + |
| 130 | +```cpp |
| 131 | +class Node { |
| 132 | + public: |
| 133 | + Node(int data) |
| 134 | + : data(data) |
| 135 | + , next(nullptr) |
| 136 | + {} |
| 137 | +} |
| 138 | + |
| 139 | +void foo(Node n) {} |
| 140 | + |
| 141 | +Node n(4); // valid |
| 142 | +foo(n); // valid |
| 143 | + |
| 144 | +Node m = 4; // valid |
| 145 | +foo(4); // valid |
| 146 | +``` |
| 147 | +
|
| 148 | +Single Parameter Constructors create implicit conversions. |
| 149 | +
|
| 150 | +``` |
| 151 | +string str = "hello"; |
| 152 | +``` |
| 153 | +
|
| 154 | +`std::string` has a 1 parameter constructor |
| 155 | +
|
| 156 | +Does an implicit conversion from `const char * --> std::string` |
| 157 | +
|
| 158 | +We can use the `explicit` keyword to disable this behavior. |
| 159 | +
|
| 160 | +```cpp |
| 161 | +class Node { |
| 162 | + public: |
| 163 | + explicit Node(int data) |
| 164 | + : data(data) |
| 165 | + , next(nullptr) |
| 166 | + {} |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +Now this will make |
| 171 | + |
| 172 | +```cpp |
| 173 | +Node m = 4; // invalid |
| 174 | +foo(4); // invalid |
| 175 | +``` |
| 176 | +
|
| 177 | +## Destructor |
| 178 | +
|
| 179 | +When an object is destroyed, a special method called the destructor runs. |
| 180 | +
|
| 181 | +Stack allocated object is destroyed when it goes out of scope. |
| 182 | +
|
| 183 | +Heap allocated object is destroyed when it is deleted. |
| 184 | +
|
| 185 | +* A class only has one destructor |
| 186 | +* 0 parameter method (no return type) |
| 187 | +
|
| 188 | +The name of a destructor is the name of the class prefixed with ~. |
| 189 | +
|
| 190 | +* You get a built-in destructor with every class. |
| 191 | + * Calls the destructor on any fields that are objects |
| 192 | +
|
| 193 | +```cpp |
| 194 | +Node *np = ...; |
| 195 | +
|
| 196 | +// np -->[1|-]-->[2|-]-->[3|/] |
| 197 | +
|
| 198 | +delete np; |
| 199 | +
|
| 200 | +struct Node { |
| 201 | + int data; |
| 202 | + Node *next; |
| 203 | + ~Node() { |
| 204 | + delete next; |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +Calling delete on `NULL` is safe. |
| 210 | + |
| 211 | +The call to a `nullptr` will be what terminates the recursive execution of the destructor. |
| 212 | + |
| 213 | +## Separate Compilation |
| 214 | + |
| 215 | +**Recall**: we split our programs into |
| 216 | + |
| 217 | +.h files (type definitions) .cc files (function implementations) |
| 218 | + |
| 219 | +In the header files, we should now include method headers. |
| 220 | + |
| 221 | +In the implementation files, we now should include method implementations. |
| 222 | + |
| 223 | +The way we implement methods is by using this notation: |
| 224 | + |
| 225 | +```cpp |
| 226 | +(Class name)::(Method name)() {} |
| 227 | + |
| 228 | +Node::Node() {} |
| 229 | +void Node::push(int val) {} |
| 230 | +int Node::pop() {} |
| 231 | +... |
| 232 | +etc. |
| 233 | +``` |
| 234 | +
|
| 235 | +## Assignment Operator |
| 236 | +
|
| 237 | +`Student bobby(billy);` copy constructor |
| 238 | +
|
| 239 | +`Student jane;` 0 parameter constructor |
| 240 | +
|
| 241 | +`jane = billy` updating an existing object to be a copy of the existing object |
0 commit comments