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 ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.