Moved to: http://www.wypblog.com/archives/184
C ++ allows you to specify the type parameter in the template class as a charming type. For example, you can assign int to the type parameter T in the general class Stack as the default type, as shown below:
Template <class T = int> class Stack {// other operator };
Now we can use the default type to declare the template class object like the following code:
Stack <> stack; // store int value
However, you must note that the default type can only be used for template classes, but not for template functions.
In the template prefix, in addition to type parameters, you can also use non-type parameters. For example, in the Stack class, you can declare the array capacity as a parameter, as shown below;
Template <class T, int capacity> class Stack {... private: T elements [capacity]; int size ;};
In this way, when creating a stack, in addition to specifying the element type, it also specifies the size of the array. For example:
Stack <int, 100> stack;
This statement declares a stack that can contain up to 100 int values.
Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
Link: some tips for using the C ++ template class (http://www.wypblog.com/archives/184)