CS 162 Lecture Notes - Lecture 9: Member Variable, Memory Management
CS 162 – Lecture 9 – Intro to the Big 3
Destructor
o Deletes the object
o Will be automatically created if one is not supplied
▪ Will not handle dynamic memory
o ~Class_Name();
▪ no return type, no parameters, only one allowed
o Called when the object goes out of scope
▪ When the function ends
▪ When the program ends
▪ A block containing local variables ends
▪ A delete operator is called
Shallow Copy vs. Deep Copy
o Shallow
▪ Copy the contents of member variables from one object to another
▪ Default for assignment operator and copy constructor
o Deep
▪ Copy what each member variable is pointing to so that you get a separate
but identical copy
▪ Has to be programmer specified
Copy Constructor
o Constructor that has one parameter that is of the same type as the class
▪ Has to be call be reference (normally const)
▪ Allows for distinct copies, changes to one does not impact the other
▪ Called automatically in three cases:
• When a class object is being declared and initialized by another
object of same type
• When a function returns a value of the class type
find more resources at oneclass.com
find more resources at oneclass.com