Chapter 13. Study Guide

C++ templates and Java generic classes are nearly identical in syntax and usage. However, C++ templates are more extensive or powerful than Java generics in two ways:

  1. They may be applied to functions outside of classes (i.e., to non-member functions)
  2. They may be used with fundamental or primitive data types like int or double in addition to classes

Much like function arguments generalize data, templates generalize data types. "Regular" function arguments act as placeholders for data passed into the function when the program calls it at runtime. Template variables are also placeholders, but the program replaces them with data type names. All valid data types, including all fundamental types like char, int, and double, and structure and class names, can replace a template variable. However, the replacement can't occur before the user specifies the replacement type. To satisfy this requirement, programmers place templates in header files where the compiler processes them after the user-specified types replace the template variables.

Know