19+-+Deep+Copies+and+Resizing

19+-+Deep+Copies+and+Resizing - EECS 280 Programming and...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Deep Copies and Resizing EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copy Constructors Deep Copies 2 When a class contains pointers to dynamic elements, copying it is tricky. If we just copy the "members of the class", we get a shallow copy. Usually, we want a full copy of everything . This is called a deep copy. There are two C++ mechanisms that copy class objects: 1. Copy Constructor: Given a formless blob of memory, and an “example” instance,
Background image of page 2
Copy Constructors Deep Copies 3 First, the copy constructor: class IntSet { int *elts; // array of elements int numElts; // number of elements in array int sizeElts; // capacity of array void copyFrom(const IntSet &is); // MODIFIES: this
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copy Constructors Deep Copies 4 Before implementing the copy constructor, think about what has to happen in copyFrom , in general , not just in the context of the copy constructor. We need to figure this out because copyFrom will be called from the assignment operator; it has to "make sense" on its own.
Background image of page 4
Copy Constructors Deep Copies 5 Here’s what it must do: 1. copyFrom is a method (like any other) and the representational invariants must hold before calling it. 2. copyFrom has to assume that the source and destination sets might have different sizes (for the assignment operator). If so, it will have to resize the array appropriately, by destroying and reallocating it.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copy Constructors Deep Copies 6 void IntSet::copyFrom(const IntSet if (is.sizeElts != sizeElts) { // Resize array delete[] elts; sizeElts = is.sizeElts; elts = new int[sizeElts]; } // Copy array for (int i = 0; i < is.sizeElts; i+
Background image of page 6
Copy Constructors Deep Copies 7 void IntSet::copyFrom(const IntSet if (is.sizeElts != sizeElts) { // Resize array delete[] elts; sizeElts = is.sizeElts; elts = new int[sizeElts]; } // Copy array for (int i = 0; i < is.sizeElts; i+ Note : The method has access to both the private data fields of the new and old instances. They are both instances of IntSet , and this implementation of an IntSet method can see both sets.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copy Constructors Deep Copies 8 void IntSet::copyFrom(const IntSet if (is.sizeElts != sizeElts) { // Resize array delete[] elts; sizeElts = is.sizeElts; elts = new int[sizeElts]; } // Copy array for (int i = 0; i < is.sizeElts; i+ Note : The old instance is accessed explicitly by mentioning the argument object name “ is ” and the new instance is accessed implicitly
Background image of page 8
Copy Constructors Deep Copies 9 Now that we have this function, the copy constructor is simple. First, we have to establish its invariants, then call copyFrom . elts = NULL; numElts = 0; sizeElts = 0; copyFrom(is);
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copy Constructors Deep Copies 10 Now that we have this function, the copy constructor is simple. First, we have to establish its invariants, then call
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 41

19+-+Deep+Copies+and+Resizing - EECS 280 Programming and...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online