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;
}

 

原文链接: https://www.cnblogs.com/lyt888/p/12488694.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/335118

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午9:59
下一篇 2023年3月1日 下午9:59

相关推荐