18-deep-copies-resize.student

18-deep-copies-resize.student - Last Time: * Creating and...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 Last Time: * Creating and destroying arrays * Classes with dynamic members: destructors * Shallow vs. deep copies Today: * implementations of copy ctor, operator==(). * Rule of the Big Three * Resizing Arrays ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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 * Copy constructor: given a formless blob of memory, and an "example" instance, make the blob a copy of the example. * Assignment operator: given existing "target" and "source" instances, make the target a copy of the source. Today, we'll implement each of these for our IntSets with dynamic arrays. Both of them depend on copying set elements from one object to another, so we factor that out. First, the copy constructor: class IntSet { int *elts int numElts int sizeElts void // MODIFIES: this // EFFECTS: copies is contents to this public: IntSet(int size = MAXELTS) ... } Before we write this, let's think about what has to happen in copyFrom, *in general*, not just in the context of the copy constructor. This is because copyFrom will be called from the assignment operator as well, so it has to "make sense" on its own.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 First, as copyFrom is a method, just like any other, the representational invariants must hold before calling it. Second, copyFrom has to assume that the source and destination sets might have different sizes. If so, it will have to resize the array appropriately, by destroying and reallocating it. { if (is.sizeElts != sizeElts) { // Resize array delete [] elts sizeElts = is.sizeElts elts = new int[sizeElts] } for (int i = 0 elts[i] = is.elts[i] } numElts = is.numElts } Note that 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 therefore see both sets of elements. The old instance is accessed explicitly by mentioning the argument object name "is" current instance on which the method operates, that is, the instance to which it is copying. +++++++++++++++++++++++++++++++++++++++++++++++++ Now that we have this function, the copy constructor is simple. First, we have to establish its invariants, then call copyFrom. Here is one way to do it: : elts (NULL), numElts (0), sizeElts (0) { copyFrom(is) } This is a bit sneaky set with no capacity to hold anything else. This is okay though---it is a valid, if not particularly useful, IntSet. The reason I chose this way was we know that copyFrom will resize the array correctly. We also could have created an empty IntSet with is.numElts elements, but I wanted to isolate any "looking inside of" the source object to copyFrom.
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 / 16

18-deep-copies-resize.student - Last Time: * Creating and...

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