1. Remember an instance
Template <class type parameter table>
Return type function name (parameter table)
{// Function Definition body}
Template <class T> T min (t x, t y );
////////////////////////////////
# Include <iostream. h>
Template <class T>/* function template declaration */
T min (t x, t y)
{If (x <Y) return X;
Else return y ;}
Void main ()
{Int n1 = 2, n2 = 10;
Double d1 = 1.5, D2 = 5.6;
Cout <"small integer:" <min (N1, N2) <Endl; // compares two integers in the instantiated min Template Function
Cout <"smaller real number:" <min (D1, D2) <Endl; // compare the two Double Precision values by instantiating the min Template Function
}
Function template min (x, y)
Template Function min (N1, N2) int type
Template Function min (D1, D2) Double Type
Class Template
Template <class type parameter table>
Class class name {
// Class definition ......
};
For example, define a class template:
Template <classt1, classt2>
Class myclass {T1 I ;//
T2 J ;//
Public:
Myclass (T1 A, T2 B )//
{I = A; j = B ;}
Void show ()
{Cout <"I =" <"J =" <j <Endl ;}};
If the main function defines a template class myclass <int, double> and declares a Class Object ob1 (1 Reference statement: myclass <int, double> OB (2, 0.1 ); note: myclass <int, double> instantiates the class template, that is, T1 instance is of the int type, T2 is of the double type, so we get a template class. then you can define the Class Object ob1 and initialize it.
You can also define another template class, such as myclass <double, char>.
Indicates the relationship between a class template and a template class.
Class template myclass <t !, T2>
Template Class myclass (INT, double)
Template Class nyclass <double, char>
Conclusion: A function template is a type of function abstraction that represents a type of function. This type of function has the same function and represents a specific function that can be called by class objects, function templates cannot be called by class objects.
A class template is an abstraction of a class that represents a class. These classes have the same functions, but the data member types and member function return types and parameter types are different. A template class is an instance of a template class. represents a specific class. class objects can be defined, but objects cannot be defined for class templates.
Http://blog.csdn.net/rufeng18/archive/2007/12/01/1910461.aspx