Chapter 26

Tempting C++ Templates

In This Chapter

arrow Examining how templates can be applied to functions

arrow Combining common functions into a single template definition

arrow Defining a template or class

arrow Implementing an initializer list for a user-defined class

The standard C++ library provides a complete set of math, time, input/output, and DOS operations, to name just a few. Many of the earlier programs in this book use the so-called character string functions defined in the include file strings. The argument types for many of these functions are fixed. For example, both arguments to strcpy(char*, char*) must be a pointer to a null-terminated character string — nothing else makes sense.

There are functions that are applicable to multiple types. Consider the example of the lowly maximum() function, which returns the maximum of two arguments. All of the following variations make sense:

  int maximum(int n1, int n2); // return max of two integersunsigned maximum (unsigned u1, unsigned u2);double   maximum (double d1, double d2);char     maximum (char c1, char c2);

I would like to implement ...

Get C++ For Dummies, 7th Edition 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.