Class template
Implementation: In class time is defined to one or more of its parameters, these parameters represent different data types. --abstract class.
When a class template is called, the specified number of parameters is generated by the data type provided by the compilation system itself, and the corresponding template class---the detailed class.
Definition of a class template
The C + + class template is written such as the following:
Template < Type tables > //Type tables are written as follows: Class type 1, class Type 2, ... class template name { member function and member variable};
A member function in a class template, as defined outside the class template,
Template < Type table > return value type class templates name < type parameter name list;:: member function name (parameter) { ...}
Use a class template to define an object's wording such as the following:
Class template Name < real type Reference > object name (constructor actual tables);
Assuming that the class template has no constructor, it can also simply write:
Class template name < real type tables > object names;
Example:
Template <class T1, class T2>class pair{public: T1 key; Keyword T2 value; Value Pair (T1 k,t2 v): Key (k), value (v) {}; BOOL operator < (const pair<t1,t2> & P) const; };template<class t1,class t2> //The member function that defines a Pair outside the class operator <bool pair<t1,t2>::operator< (const PAIR<T1, T2> & P) Const {return key < P.key;}
Use of the pair class template:
int main () { pair<string, int> student ("Tom", +); Instantiate a class pair<string, int> cout << student.key << "" << Student.value; return 0;}
function templates as class template members
#include <iostream>using namespace Std;template <class t>class a{public: template<class T2> void Func (T2 t) {cout << t;}//member function template};int main () { a<int> A; A.func (' K '); The member function template func is instantiated with return 0;}
Class templates and non-type parameters
Template <class T, int size>class carray{ t array[size];p ublic: void Print () {for (int i = 0; I &l T Size ++i) cout << array[i] << Endl; } ;
Carray<double, 40> A2; Carray<int, 50> A3;
Note:
Carray<int,40> and carray<int,50> are totally two classes.
The objects of these two classes cannot be assigned to one another
Class templates and inheritance
Generic classes derive from template classes
Template <class t>class A {T v1; int n; };class b:public a<int> {double v; };/ /Class A is a class template. A<int> is the template class int main () { B obj1; return 0;}
Class templates derive from template classes
Template <class T1, class T2>class A {T1 v1; T2 v2; }; Template <class t>class b:public a<int, double> {T v;}; int main () { b<char> obj1; return 0;}
Class template A, instantiation template class a<int,double>, derived class template B, instantiation template class b<char>
There are members in B at this time
{
int v1;
Double v2;
T v;
}
Class templates derive from ordinary classes
Class A { int v1;}; Template <class t>class b:public A {T v;}; int main () { b<char> obj1; return 0; }
There's nothing to say. A member of type T was added on the basis of inheriting a.
Class templates derive from class templates
#include <iostream>using namespace Std;template <class T1, class T2>class a{public: T1 v1; T2 v2;}; Template <class T1, class T2>class b:public a<t1,t2>{public: T1 v3; T2 v4;}; int main () { b<int,double> B; b<int,double> *PB = &b; B.V1 = 1; B.v3 = 3; B.v2 = 2.2; B.V4 = 4.4; cout << pb->v1<<endl; cout << pb->v2<<endl; cout << pb->v3<<endl; cout << pb->v4<<endl; return 0;}
Be sure to instantiate the class template B when it is instantiated. Class template A also ends with the instantiation of type T.
B<int,double> B, B has its own member int v3, double V4 also has the member of the Inheritance a<int,double> int v1 and double v2
Preliminary C + + class template Learning Notes