Class template
When defining a class, you can also abstract some types and replace them with template parameters, making the class more versatile. This class is called a template class, for example:
Template <typename t> class A
{
T data;
Public
How class template member functions are defined one: defined in a class template
void print () {cout << data << Endl;}
}
How class template member functions are defined two: outside the class template
template<typename t> void A::p rint () {cout <<data << Endl;}
Class Templates (2)
After class templates instantiate a class, the class is instantiated as an object
Class template
Template parameters for the class template
1. Type parameters: Using TypeName or class tags
2. Non-type parameter: Integer, enumeration, pointer (to object or function), reference (Reference object or reference function). Where the integer type is more commonly used, such as:
Template<typename t,unsigned size>
Class Array
{
T Elems[size];
...
}
Array<char,10> array0;//defining an object with a class template instance
Template parameter is another class template
The corresponding declaration form is as follows:
Template<typename t,template<typename tt0,typename tt1> class a>
struct FOO
{
A<t,t> Bar;
};
C + + Programming Method 4: Class template