派生类访问基类静态函数

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class base
 6 {
 7 public:
 8     int num;
 9     static int data;
10     void print()
11     {
12         cout << "base print" << (this->num) << end;
13     }
14 
15     static void printstatic()
16     {
17         // 静态函数不可以引用内部成员 所以不能打印(this->num)
18         
19         cout << data << endl;// 可以
20         cout << "base printstatic" << (this->num) << end;
21     }
22 };
23 
24 int base::data = 100;
25 
26 // 操作静态成员  尽量用静态函数
27 class newbase : public base
28 {
29 
30 };
31 
32 void main()
33 {
34     cout << typeid(&base::print).name() << ensl;
35     
36     cout << typeid(&base::printstatic).name() << ensl;
37 
38     newbase *p = new newbase;
39 
40     p->printstatic();// 
41 
42     newbase::printstatic();// 派生类访问基类静态成员的方法
43 
44     cin.get();
45 }

 

posted on 2015-06-08 22:21  Dragon-wuxl  阅读(270)  评论(0)    收藏  举报

导航