类模板与静态成员

 1 /* 类模板与静态成员 */
 2 
 3 
 4 // 对于static成员,每一个实例化的类的静态成员都是互相独立的
 5 
 6 #include<iostream>
 7 
 8 using namespace std;
 9 
10 template<class T>
11 class myclass
12 {
13 public:
14     static int num ;
15     myclass()
16     {
17         num+=1;
18     }
19 
20     ~myclass()
21     {
22         num-=1;
23     }
24 
25 
26     void print()
27     {
28         cout << "num = " << num << endl;
29         cout << typeid(*this).name() << " " << (void*)&num << endl;// 地址不一样  说明不是同一个变量
30     }
31 
32 };
33 
34 template<class T>
35 int myclass<T>::num = 0;
36 
37 
38 void main()
39 {
40     myclass<int> *p = new myclass<int>[10];
41     p->print();
42 
43 
44     myclass<double> *pd = new myclass<double>[8];
45     pd->print();
46 
47 
48 
49     cin.get();
50 }

 

posted on 2015-06-12 16:34  Dragon-wuxl  阅读(123)  评论(0)    收藏  举报

导航