一、子类对象无法直接调用父类静态变量,必须在子类中也声明
#include<iostream>
#include<string>
using namespace std;
class Base1 {
public:
static string name;
};
string Base1::name = "hello world";
class Son1 : public Base1 {
public:
//如果不写此行,则通过子类初始化静态变量时 会报错
//如果写此行,则必须在下面初始化
static string name;
};
//ERROR“Base1::name”: 静态数据成员无法通过派生类初始化
string Son1::name = "world hello";
int main() {
Son1 s1;
cout << s1.Base1::name << endl; //相当于Java中当前类对象中调用super
cout << s1.name << endl;
system("pause");
return 0;
}