C++继承中的同名静态成员处理

#include<iostream>
using namespace std;
class Base {
public:
    static int m_A;
    static void func() {
        cout << "Base static func()调用" << endl;
    }
    static void func(int a) {
        cout << "Base static func(int a)调用" << endl;
    }
};
int Base::m_A = 100;

class Son :public Base {
public:
    static int m_A;
    static void func() {
        cout << "Son static func()调用" << endl;
    }
};
int Son::m_A = 200;

void test01() {
    Son s;
    cout << "通过对象访问" << endl;
    cout << "Son m_A = " << s.m_A << endl;
    cout << "Base m_A = " << s.Base::m_A << endl;
    cout << "通过类名访问" << endl;
    cout << "Son m_A = " << Son::m_A << endl;
    cout << "Base m_A = " << Base::m_A << endl;
    cout << "Base m_A = " << Son::Base::m_A << endl;
}
void test02() {
    Son s;
    cout << "通过对象访问" << endl;
    s.func();
    s.Base::func();
    s.Base::func(100);
    cout << "通过类名访问" << endl;
    Son::func();
    Son::Base::func();
    Son::Base::func(100);
}
int main() {
    test01();
    cout<<"----------------------"<<endl; 
    test02();
    system("pause");
    return 0;
}

 

posted on 2020-03-13 20:12  ~明月几时有  阅读(281)  评论(0)    收藏  举报