Show: Object Pascal C++
Display Preferences

Class Templates

From Appmethod Topics
Jump to: navigation, search

Go Up to Class Templates Overview Index

A class template (also called a generic class or class generator) lets you define a pattern for class definitions. Consider the following example of a vector class (a one-dimensional array). Whether you have a vector of integers or any other type, the basic operations performed on the type are the same (insert, delete, index, and so on). With the element type treated as a type parameter to the class, the system will generate type-safe class definitions on the fly.

As with function templates, an explicit template class specialization can be provided to override the automatic definition for a given type:

class Vector<char *> { ... };

The symbol Vector must always be accompanied by a data type in angle brackets. It cannot appear alone, except in some cases in the original template definition.

See Also

Personal tools
In other languages