CS246 Lecture Notes - Lecture 16: Stack-Based Memory Allocation, Object Copying, Memory Management
Document Summary
October 27 -- the big three (copy ctor, dtor, assignment operator) Member initialization list (mil): necessary to initialize const & ref fields. Creating new object as a copy of another calls the copy ctor. Every class comes with built in copy ctor. A copy ctor is a ctor that takes one parameter, a reference to const of the type of the class. Does field for field assignment struct student{ int assns, int, final; Student(const student &other): assns(other. assns),mt(other. mt),final(other. final){ } struct node{ int data; Node *np = newnode(1, new node(2, new node(3, null))); Node m = *np; // calls copy ctor m on stack. Node *npcopy = new node(*np); // calls copy ctor on heap. Want a deep copy when class contains at least one field that is dynamically allocated/not continuous in memory. Typically want a deep copy if the object uses dynamically allocated memory (not contiguous in memory)