From the course: C Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Template classes

Template classes

- [Instructor] Template classes are useful for a lot of purposes, and they're commonly used for operating on containers. This is template-class.cpp from Chapter 08 of the Exercise Files. This is a simple Last-In-First-Out LIFO stack. There's a more complete version in the standard template library, but I've created this simple version so we can see how it works in the context of our discussion of templates. The important thing to notice about this is that there is absolutely no code here that cares about the type of the items on the stack. So let's take a look at the class. The class starts here around line 23 or line 24. We have the template keyword, we have typename, and the type place holder, capital T. So the type of the elements on the stack is going to be this type T. We have a couple of static constant. Static constant in the context of a class means that there's only one copy of them no matter how many…

Contents