C++中子类是否可以继承父类的静态成员和静态成员函数?
静态成员变量
#include <iostream>
using namespace std;
class Base {
public:
Base(int a) {
val = a;
cout << "Base construct fun" << endl;
}
static int num;
int val;
};
int Base::num = 8;
class Son : public Base {
public:
Son(int a) : Base(a) {
cout << "Son construct fun" << endl;
}
};
int main() {
Base base(1);
Son son(2);
cout << "base val:" << base.val << endl;
cout << "son val:" << son.val << endl;
cout << "base num:" << base.num << endl;
cout << "son num:" << son.num << endl;
cout << "================" << endl;
son.num = 9;
son.val = 3;
cout << "base val:" << base.val << endl;
cout << "son val:" << son.val << endl;
cout << "base num:" << base.num << endl;
cout << "son num:" << son.num << endl;
return 0;
}
从结果来看,子类可以调用父类的静态成员变量,但又和普通的成员变量继承不同的是,我们通过修改子类的静态成员变量的值,父类调用静态成员变量也被修改了。其实该静态成员只有唯一的一个实例。 可以看做子类共享父类的静态成员的内存,子类可以修改该内存中的值,因此父类调用时也被修改了。 所以并不能看成继承关系,可以认为是共享关系。
子类是否可以覆盖基类的静态成员呢?
#include <iostream>
using namespace std;
class Base {
public:
Base(int a) {
val = a;
cout << "Base construct fun" << endl;
}
static int num;
int val;
};
int Base::num = 8;
class Son : public Base {
public:
Son(int a) : Base(a) {
cout << "Son construct fun" << endl;
}
static int num;
};
int Son::num = 9;
int main() {
Base base(1);
Son son(2);
cout << "base val:" << base.val << endl;
cout << "son val:" << son.val << endl;
cout << "base num:" << base.num << endl;
cout << "son num:" << son.num << endl;
cout << "================" << endl;
son.num = 10;
son.val = 3;
cout << "base val:" << base.val << endl;
cout << "son val:" << son.val << endl;
cout << "base num:" << base.num << endl;
cout << "son num:" << son.num << endl;
return 0;
}
在子类重新定义一个相同的静态成员变量,可以看出,子类和父类的静态成员变量其实是两个不同的东西了(开辟了两块静态变量空间),修改子类并不会修改父类中的值。
静态成员函数
#include <iostream>
using namespace std;
class Base {
public:
Base(int a) {
val = a;
cout << "Base construct fun" << endl;
}
static void fun() {
cout << "Base static fun" << endl;
}
int val;
};
class Son : public Base {
public:
Son(int a) : Base(a) {
cout << "Son construct fun" << endl;
}
};
int main() {
Base base(1);
Son son(2);
base.fun();
son.fun();
return 0;
}
看起来是子类继承了父类的静态成员函数
#include <iostream>
using namespace std;
class Base {
public:
Base(int a) {
val = a;
cout << "Base construct fun" << endl;
}
static void fun() {
cout << "Base static fun" << endl;
}
int val;
};
class Son : public Base {
public:
Son(int a) : Base(a) {
cout << "Son construct fun" << endl;
}
static void fun() {
cout << "Son static fun" << endl;
}
};
int main() {
Base base(1);
Son son(2);
base.fun();
son.fun();
return 0;
}
覆盖也是可以实现的 多态呢
#include <iostream>
using namespace std;
class Base {
public:
Base(int a) {
val = a;
cout << "Base construct fun" << endl;
}
static void fun() {
cout << "Base static fun" << endl;
}
int val;
};
class Son : public Base {
public:
Son(int a) : Base(a) {
cout << "Son construct fun" << endl;
}
static void fun() {
cout << "Son static fun" << endl;
}
};
int main() {
Base base(1);
Son son(2);
Base* p = new Son(3);
base.fun();
son.fun();
p->fun();
return 0;
}
明显多态无法实现(static不可能是虚函数,这个很好理解)
|