Bootstrap

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

#include <iostream>

using namespace std;

//继承中静态成员的处理;
//类似非静态成员的处理;

class Base
{
public:
	static void func()
	{
		cout << "调用func()" << endl;
	}
	static void func(int a)
	{
		cout << "调用func(int a)" << endl;
	}
	static int m_A;
};
int Base::m_A = 10;//静态变量,类内声明,类外初始化;

class Son1 : public Base
{

};
void test01()
{
	Son1 s1;
	cout << Son1::m_A<< endl;
}
//静态成员属性子类可以继承下来;

class Son2 : public Base
{
public:
	static int m_A;
};
int Son2::m_A = 20;
void test02()
{
	cout << Son2::m_A << endl;
}
//会将父类的同名成员隐藏;

class Son3 :public Base
{
public:
	static void func()
	{
		cout << "调用son3 func()" << endl;
	}
	static void func(int a)
	{
		cout << "调用son3 func(int a)" << endl;
	}
};
void test03()
{
	Son3 s3;
	s3.func();
	s3.func(1);
}

int main()
{
	test01();
	test02();
	test03();
    return 0;
}

 

;