1 #include <iostream> 2 using namespace std; 3 4 //static成员,每个类型都会实例化,创建一个变量,类型一致则共享,否则不共享 5 6 template <class T> 7 class myclass 8 { 9 public: 10 static int num; 11 myclass() 12 { 13 num += 1; 14 } 15 ~myclass() 16 { 17 num -= 1; 18 } 19 void show() 20 { 21 cout << "num:" << num << endl; 22 cout << typeid(*this).name() << endl; 23 } 24 }; 25 26 template<class T> 27 int myclass<T>::num = 0; 28 29 void main() 30 { 31 myclass<int> *p = new myclass<int>[10]; 32 p->show(); 33 34 myclass<double> *p2 = new myclass<double>[19]; 35 p2->show(); 36 cin.get(); 37 }