上一篇讲解的是非静态成员,在继承中的处理。
本篇主要讲解静态成员在继承中的处理。
其实静态成员和非静态成员同名时,在使用的方法上是一样的。
下面举例说明:
/****************************************************
* brief : 继承中静态成员的处理
* author : shao
* date : 2020-04-02
* note : 对于重名的成员,子类调用时和非静态成员的
* 使用方式一样。
*
****************************************************/
#include <iostream>
using namespace std;
class Base{
public:
static int m_A;
static void func()
{
cout << "Base func()" << endl;
}
static void func(int a)
{
cout << "Base func(int a)" << endl;
}
};
int Base::m_A = 10;
class Son : public Base{
public:
static int m_A;
static void func()
{
cout << "Base func()" << endl;
}
};
int Son::m_A = 20;
void test01(void)
{
Son s;
//调用子类中的m_A
cout << Son::m_A << endl;
//当想调用父类中的m_A时
cout << Base::m_A << endl;
//或者用下面的方式
cout <