Creating Class Templates Class templates are useful for creating abstract data types, which can then be used to create classes based on specific data types. For example, you can use a linked list template to parameterize the data that is held at each of the link list's nodes. You can use class templates to create collection templates, create new data types, and modify existing data types. This section includes the following topics: Class Template Syntax Class templates are composed of three elements: ® The template keyword ® A comma-delimited list of formal parameters, enclosed in angle brackets ( <> ) ® The class definition A class that is generated from a template is sometimes called a templated class. You can use class templates to modify existing classes. For example, you can define a class template that adds reference counting to an object as follows: template <class T> class TCount { int m_count; T m_type; public: TCount(
Comments
Post a Comment