CMPT 225 Lecture Notes - Lecture 2: Memory Management, Object Copying, Memory Leak
Document Summary
Write a new copy constructor that will create a deep copy. Need to give it an object of the same type as the type i"m building const means that the parameter i"m passing can"t change - copy constructor can"t accidentally destroy thing i am making a copy of. & passes by reference (not making a copy when we pass it to copy constructor, just giving it address) Mystack::mystack(const mystack & st) n = st. n; top = st. top; Resposible for cleaning up any dynamic memory associated with an object. Much like in push, want to delete array so we don"t cause a memory leak. Never explicitly call a destructor, they are called for you. Basically just example of writing a function that will return a stack. void assigntest() Mystack st = stack123(); cout << st. pop() << endl; cout << st. pop() << endl; cout << st. pop() << endl; It doesn"t work and just spits out garbage values!