O'Reilly logo

C++ All-In-One For Dummies®, 2nd Edition by Jeff Cogswell, John Paul Mueller

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

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

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