类模板与静态函数

 1 /* 类模板与静态函数 */
 2 
 3 /* 类模板静态函数 */
 4 
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 // 类,类模板的静态成员函数不可以访问this
10 // 类模板没有实例化 没有调用就不会编译
11 
12 template<class T>
13 class myclass
14 {
15 public:
16     int num;
17     static int data;
18     T t;
19     
20     myclass()
21     {
22         num+=1;
23     }
24 
25     ~myclass()
26     {
27         num-=1;
28     }
29 
30     void printnum()
31     {
32         cout << "num = " << this->num << endl;
33     }
34     
35 
36     static void print()
37     {
38         //类,类模板的静态成员函数不可以访问this
39         //cout << "static" << "num = " << this->num << endl;
40         
41         cout << "static" << "  " << data << endl;
42     }
43 
44 
45 
46 
47 };
48 
49 template<class T>
50 int myclass<T>::data = 0;
51 
52 void main()
53 {
54     myclass<int> my1;
55     myclass<int> my2;
56     myclass<int> my3;
57     my1.print();
58 
59     auto fun1 = &myclass<int>::print;// 类模板静态函数必须指定类型
60     
61     auto fun2 = &myclass<int>::printnum;
62     
63     cout << typeid(fun1).name() << endl;
64 
65     cout << typeid(fun2).name() << endl;
66 
67     fun1();
68 
69     //my1.*fun2(); 
70 
71     myclass<double> my11;
72     myclass<double> my12;
73     myclass<double> my13;
74     my13.print();
75 
76     cin.get();
77 }

 

posted on 2015-06-12 17:14  Dragon-wuxl  阅读(421)  评论(0)    收藏  举报

导航