17+-+Deep+Copies+and+Resizing

17+-+Deep+Copies+and+Resizing - 11/16/2009 Copy...

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

View Full Document Right Arrow Icon
11/16/2009 1 Deep Copies and Resizing EECS 280 Programming and Introductory Data Structures 1 Copy Constructors Deep Copies 2 c When a class contains pointers to dynamic elements, copying it is tricky. c If we just copy the "members of the class", we get a shallow copy. c Usually, we want a full copy of everything . c This is called a deep copy. c There are two C++ mechanisms that copy class objects: 1. Copy Constructor: Given a formless blob of memory, and an “example” instance, make the blob a copy of the example. 2. Assignment operator: Given existing "target" and "source" instances, make the target a copy of the source. Copy Constructors Deep Copies 3 c 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 // EFFECTS: copies is contents to this public: IntSet(int size=MAXELTS); // client optionally // names size IntSet(const IntSet &is); // copy constructor ... }; Copy Constructors Deep Copies 4 c Before implementing the copy constructor, think about what has to happen in copyFrom , in general , not just in the context of the copy constructor. c We need to figure this out because copyFrom will be called from the assignment operator; it has to "make sense" on its own. Copy Constructors Deep Copies 5 c 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. Copy Constructors Deep Copies 6 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++) { elts[i] = is.elts[i]; } // Establish numElts // invariant numElts = is.numElts; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
11/16/2009 2 Copy Constructors Copy Constructors Deep Copies Deep Copies 7 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++) { elts[i] = is.elts[i]; } // Establish numElts // invariant numElts = is.numElts; } 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. Copy Constructors Copy Constructors Deep Copies Deep Copies 8 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++) { elts[i] = is.elts[i];
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

17+-+Deep+Copies+and+Resizing - 11/16/2009 Copy...

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

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