Page 315 - DCAP404 _Object Oriented Programming
P. 315
Object-oriented Programming
Notes {
X array [ var ] ;
}
};
14.3 The Typename Keyword
The keywords typename and class can be freely interchanged. For example
Template < class T >
Int maxindex ( T arr [ ] , int size )
{
. . .
}
14.4 Template Specialization
While template functions and classes are usable for any data type, that would hold true only, as
long as the body of the functions or the class is identical throughout. So, if you have,
Template < typename T .
Void swap ( T + Ihs , T + rhs )
{
T tmp ( + Ihs ) ;
+ Ihs = + rhs ;
+ rhs = tmp ;
}
the previous template will be instantiated correctly for any of the pointer types passed to it -
except for the data types char + , unsigned char + , and signed char + . Since, the body of the
function.
!
Caution Differs for these types, you would use template specialize the template function.
Specialization would consist of instantiating the template definition for a specific data type. So,
when the compiler needs to instantiate the template, it finds a predefined version already
existing and uses it. For example, just after the previous template declaration, you could create
a template specialization for char + like this:
Template e >
Void swap < char > ( char + Ihs , char + rhs )
{
char + tmp = new char [ strlen (Ihs) + 1 ] ;
strcpy ( tmp , Ihs) .;
strcpy ( tmp , Ihs);
308 LOVELY PROFESSIONAL UNIVERSITY