静态成员和非静态成员出现同名处理方式

静态成员和非静态成员出现同名处理方式
访问子类同名成员,直接访问
访问父类成员需要加作用域

 

 1 #include<iostream>
 2 #include <string >
 3 using namespace std;
 4 
 5 class a
 6 {
 7 public:
 8 static int m_A;
 9 };
10 int a::m_A = 100;
11 class b: public a
12 {
13 public:
14 static int m_A;
15 };
16 int b::m_A = 200;
17 
18 int main()
19 {
20 
21 b p1;
22 //通过对象访问
23 cout << p1.m_A << endl;
24 cout << p1.a::m_A << endl;//类作用域访问
25 cout << b::m_A << endl;//通过类名作用域访问
26 cout << b::a::m_A << endl;//通过类名作用域访问
27 return 0;
28 }

 

posted @ 2021-02-18 23:31  两天阿来  阅读(177)  评论(0)    收藏  举报