一、基类中有虚函数但不是虚继承
class A {
public:
A() {
c();
}
virtual ~A() {
d();
}
virtual void c() {
cout << "Construct A\n";
}
virtual void d() {
cout << "Deconstruct A\n";
}
};
class B : public A {
public:
B() { // 等价于 B() : A(){}
c();
}
~B() {
d();
}
void c()override {
cout << "Construct B\n";
}
void d()override {
cout << "Deconstruct B\n";
}
};
int main() {
B b;
return 0;
}
Construct A
Construct B
Deconstruct B
Deconstruct A
二、有虚继承
Linux 布局参考这里
参考: