A template is a tool that parameterized data types. Templates can be divided into function templates and class templates.
When defining a template, some function parameters or data member types are not described, but their data types are used as template parameters. When using the template, the template parameter is determined based on the data type of the real parameter to obtain an instance of the template.
Function Template)
A function template is a function that does not describe the data type of certain parameters. For example, the following defines a function template that can perform operations (absolute values) on any type of variables.
Template <class T> // or written as: Template <typename T> t abs (T Val) {return Val <0? -Val: Val ;}
The template definition starts with the keyword template. The keyword class (or typename) indicates that the identifier T is the template parameter (type parameter), which is used to specify the function template ABS () the data type of the parameter Val, followed by the ABS () definition of the function template. When defining a function template, the parameterized data type T can be used to declare the parameters and return values of the function, and can also be used to declare work variables in the function body.
When a function template is called, the compiler determines the type of the template parameter T based on the actual parameter type, and automatically generates a corresponding function.
The function template is defined and used as follows.
# Include <iostream. h> template <class T> // or write it as template <typename T> t abs (T Val) {return Val <0? -Val: Val;} void main () {int I = 100; cout <ABS (I) <Endl; // replace type parameter T with intlong L =-12345l; cout <ABS (l) <Endl; // replace type parameter T with longfloat F =-125.78f; cout <ABS (f) <Endl; // replace type parameter T with float}
You can also use multiple type parameters when defining a function template. In this case, the class or typename keyword must be added before each type parameter and separated by commas. The format is as follows.
Template <class T1, class T2, class T3>
The following is an example of a function template that uses multiple types of parameters.
# Include <iostream. h> template <class T1, class T2> T1 max (T1 X, T2 y) {return x> = y? X :( T1) y;} void main () {int I = 100; float F = 125.78f; cout <max (I, f) <Endl; // replace T1 with int, and T2 with float}
Class Template)
A class template is a common class. when defining a class, it does not describe the parameters of some data members, member functions, and the Data Type of the returned values. Class is the abstraction of objects, while class templates are the abstraction of classes and at a higher level. A class template is also called a class factory with parameters ), it can be used to generate parameters of different data types that have the same members and some data members, member functions, and returned values.
Class templates use the type parameter t of class templates when defining their member functions. The following example defines a template containing a type parameter.
Template <class T> // or written as: Template <typename T> class mytemclass {PRIVATE: t x; // type parameter T is used to declare the data member public: void setx (t) {x = A;} // type parameter T is used to declare the parameter t getx () {return X;} // type parameter T is used to declare the return value of the member function };
If the member functions of the template class are defined externally in the template class, the following form must be used:
Template <class T> // The template declaration mytemclass <t >:: setx (t a) {x = A;} cannot be omitted ;}
The class template determines the Data Type of the type parameter by using the actual data type given when the class template declares the object. The following uses the class template with the type parameter 'int' to declare an object 'object.
Mytemclass <int> jsonbject;
For the above object declaration, the compiler first replaces the type parameter T in the class template with int, generates a class (Template Class) of all data types, and then declares an object using this class.
When defining a class template, multiple type parameters can be used for the same amount, and the type parameters of the class template can contain parameters of the determined type, as shown below:
Template <class T1, int I, Class T2> // contains the iclass mytemclass {... // member declaration of the class template of the determined type };
In this case, the object of the Declaration class template should adopt the following form:
Mytemclass <int, 100, float> myobject;
The following is an example of a class template that uses multiple types of parameters.
# Include <iostream. h> template <class T1, class T2> // use two type parameters class mytemclass // define the class template {PRIVATE: T1 X; T2 y; public: mytemclass (T1, t2 B) {x = A; y = B;} // constructor void showmax () // output the largest data member {cout <"maxmember =" <(x> = y? X: Y) <Endl ;}}; void main () {int A = 100; float B = 123.45f; mytemclass <int, float> MT (A, B ); // declare the object Mt of the class template. showmax ();}