一、基类中有虚函数但不是虚继承

C++-虚函数表派生类内存布局

C++-派生类有虚函数时的内存布局

C++-无虚基类多继承派生类存储空间

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 布局参考这里

C++-虚继承存储空间

C++-虚继承存储空间1

C++-虚继承存储空间2

参考:

  1. Memory Layout of C++ Object in Different Scenarios
  2. All About Virtual Keyword in C++: How Does Virtual Function Works Internally?
  3. All About Virtual Keyword in C++: How Does Virtual Base Class Works Internally?