lecture17 - Last Time: * Creating and destroying arrays *...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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: We could declare a copy constructor for our IntSet class as follows class IntSet { int *elts; // array of elements int numElts; // number of elements in array int sizeElts; // capacity of array public: IntSet(int size = MAXELTS); // client optionally names size IntSet(const IntSet &is); // copy constructor ... }; The copy constructor is invoked on a "formless" instance of an IntSet, and must make the "formless" version look like an exact copy of the argument. The argument must be passed by reference to avoid infinite recursion. The argument is const for two reasons. First, if you are passing something by value, the act of passing it had better not change it! Second, this ensures that *any* instance of the class can serve as the source, not just a mutable one. The the copy constructor has to accomplish the following tasks: * allocate an array of the same size as the source set's * copy each element from the source array to the new array * copy the numElts/sizeElts fields The copying part is going to have to happen in both the copy constructor and the assignment operator. So, we will abstract away the copying into a utility function. This adds a private method to our ADT: class IntSet { int *elts; // array of elements int numElts; // number of elements in array int sizeElts; // capacity of array void copyFrom(const IntSet &is); // REQUIRES: this has enough capacity to hold is // MODIFIES: this // EFFECTS: copies is contents to this public: IntSet(int size = MAXELTS); // client optionally names size IntSet(const IntSet &is); // copy constructor ......
View Full Document

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 7

lecture17 - Last Time: * Creating and destroying arrays *...

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