The Member Functions Required for Classes with Dynamic Memeory.
- Most references are from the book, chapter 14.
- But for some reason, she does not cover the assignment operator.
- this article brings in all the pieces.
- You can ignore the exception part for now.
- I am going to be somewhat inefficient with my code
- I am not going to worry about redundant code right now.
- I want to see the operations with the associated code.
- You could REALLY clean this up later.
- Implementing the dynamic array.
- Just in case
- A shallow copy is an operation that copies one object to another without copying any pointed to data.
- A shallow copy only copies static memory.
- A shallow copy is the default for
- The assignment operator.
- The copy constructor.
- A deep copy is an operation that not only copies one object to another but also makes copies of any pointed to data.
- A deep copy copies dynamic as well as static memory.
- To make classes work with dynamic memory we need
- A destructor
- This frees the dynamic memory when the instance is destroyed
- This prevents memory leaks.
- A copy constructor
- This is called on pass by value
- It can be called at other times.
- This function makes a deep copy from the argument into the parameter
- The copy constructor prevents a dangling pointer when the descturctor of the parameter is called.
- An overloaded assignment operator
- Prevents dangling pointers and memory leaks.
- This operator makes a deep copy from the rhs to the lhs.
- Look at experiment.C