O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.14. Putting It All Together

Container Classes, Pointers, and Copy Semantics

The template List class in “A Generic List Class with Iterators and Value Semantics” on page 460 uses the following function to copy values for overloaded assignments and copy initialization.

Listing 11.25. Copy operation for value semantics Lists
template <class TYPE>
void List<TYPE>::copy(const List<TYPE> & list) {      // copy List
   for (Iterator<TYPE> iter(list); !iter; iter++)
      append(iter());                     // make copy of TYPE
}

An iterator steps through the source List object, and append() creates a copy of each contained object. List provides value semantics and Node objects contain data; hence, this algorithm is correct. The following code fragment declares two Lists of Strings ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required