#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
static void func()
{
cout << "Base-static void func()" << endl;
}
static void func(int a)
{
cout << "Base-static void func(int a)" << endl;
}
static int m_A;
};
int Base::m_A = 100;
class Son :public Base
{
public:
static void func()
{
cout << "Son-static void func()" << endl;
}
static int m_A;
};
int Son::m_A = 200;
//同名成员属性
void test01()
{
cout << "通过对象访问" << endl;
Son s;
cout << s.m_A << endl;
cout << s.Base::m_A << endl;
cout << "通过类名访问" << endl;
cout << Son::m_A << endl;
cout << Son::Base::m_A << endl;
}
//同名成员函数
void test02()
{
cout << "通过对象访问" << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问" << endl;
Son::func();
Son::Base::func();
//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作用域访问
Son::Base::func(100);
}
int main()
{
test01();
test02();
return 0;
}
C++继承同名静态成员处理方式
最新推荐文章于 2024-07-14 13:39:50 发布