Guidelines For Templates
- Templates are applicable when we want to create a type secure class that can handle different data types with similar member functions.
- The template classes can also be involved in inheritance. For example,
Both data and base are template classes. The class data are derived from the template class base.
- The template variables also allow us to assign default values. For example,
- The name of the template class is written differently in various situations. When it is a class declaration, it is declared in the following manner:
Finally, while declaring objects, the class name and specific data are specified before the object name.
- All template arguments declared in the template argument list should be used for the definition of formal arguments. If one of the template arguments is not used, the template will be specious. Consider the following example:
In the above example, the template argument T is not used as a parameter, and the compiler will report an error.
In the above example, the template-type argument is not used as an argument. The system will probably crash.
The template variable S is not used. Therefore, compile time error is generated.