Chapter 5. Creating Classes with Templates

In This Chapter

  • Creating class templates

  • Using parameters in templates

  • Deriving with templates

  • Creating function templates

If C++ programming has any big secret, it would have to be templates. Templates seem to be the topic that beginning programmers strive to understand because they've heard about them and seem to think that templates are the big wall over which they must climb to ultimately become The C++ Guru.

We can't say whether understanding templates will make you a C++ guru (we like to think that it will!), but we can say that it will open your abilities to a whole world out there, primarily because the entire Standard C++ Library is built around templates. Further, understanding templates can help you understand all that cryptic code that you see other people posting on the Internet. (And it will help you realize that it didn't have to be so cryptic! Simplify, simplify, we always say!)

So, in this chapter, we show you how to program templates in C++.

Tip

Templates have an interesting history. Back when people started building C++ compilers, no standard for how to do templates existed. As a result, different compilers supported different ways of doing templates. The whole template thing was a mess and very confusing. So if you're using a compiler that's older than around 1999 or 2000 and you're interested in using templates, you should seriously consider upgrading to a newer compiler (such as the GNU gcc compiler included with CodeBlocks ...

Get C++ All-In-One For Dummies®, 2nd 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.