{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture17 - Last Time Creating and destroying arrays...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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 ... }; 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern