码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-13 20:26:59      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:turn   space   std   public   ace   pac   nbsp   ret   处理   

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

 

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

标签:turn   space   std   public   ace   pac   nbsp   ret   处理   

原文地址:https://www.cnblogs.com/lyt888/p/12488694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!