//the static keyword in the class template#include <iostream>using namespacestd;/*The class template is essentially a C + + compiler that creates different classes based on the type parameters, C + + The compiler generates a static variable for each class when the class is generated using the class template then it is very good for the static keyword in the class to understand that the static keyword-decorated variable is a static static variable of an object-shared class belonging to the same class of the class The variable data type of the static decoration in a class template must be determined not to be a type parameter because a static variable is initialized before the class object the class class is not generated by the class template*/Template<typename t>classpoint{ Public: Point (T a,t b) { This->x =A; This->y =b; } Static intTotal ; Static voidPrint () {cout<<"total="<< Total <<Endl; }Private: T x; T y;};//the only way to initialize static variables in a classTemplate<typename t>intPoint<t>::total =Ten;voidProtecta () { point<int> P1 (3,3); P1.total= A; Point<int> P2 (5,5); Point<float> P3 (4.0,4.0); P3.total= -; //share a static variable with the same classP2. Print (); //different classes do not share a static variablepoint<float>::P rint ();}voidMain () {protecta (); System ("Pause");}
C + + class template three (static keyword in class templates)