123.static静态函数和函数模板

 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 }

 

posted @ 2018-03-21 17:55  喵小喵~  阅读(532)  评论(0编辑  收藏  举报