C++静态数据成员的继承
基类及其所有子类共用该基类的同一个静态成员变量
真正理解:静态成员从属于类
测试代码:
TestStatic.h:
class TestStatic
{
public:
static int y;
};
class TestStaticChild :
public TestStatic
{
};
class TestStaticChild2 :
public TestStatic
{
};
main.cpp:
#include "TestStatic.h"
#include <iostream>
using namespace std;
int TestStatic::y = 10;
int main() {
TestStatic test;
TestStaticChild test1;
TestStaticChild2 test2;
TestStatic::y = 1;
cout << test.y << endl;
TestStaticChild::y = 2;
cout << test1.y << endl;
TestStaticChild2::y = 3;
cout << test2.y << endl << endl;
cout << test.y << endl;
cout << test1.y << endl;
cout << test2.y << endl;
system("pause");
return 0;
}