#include <iostream>
using namespace std;
class A
{
public:
virtual void func_1() {}
virtual void func_2() {}
virtual ~A() {}
};
class B
{
public:
virtual void func_3() {}
};
class C : public B
{
public:
virtual void func_3() {}// 覆盖
virtual void func_4() {}
};
class D : public A, public B
{
public:
virtual void func_1() {}
virtual void func_5() {}
};
int main()
{
// 断点调试查看虚表
A a;
B b;
C c;
D d;
cout << "sizeof(A) = " << sizeof(A) << endl;//sizeof(A) = 4,因为无论有多少虚函数,A实例只有一个指向虚表的指针
cout << "sizeof(B) = " << sizeof(B) << endl;//sizeof(A) = 4,理由同上
cout << "sizeof(C) = " << sizeof(C) << endl;//sizeof(A) = 4,单一继承
cout << "sizeof(D) = " << sizeof(D) << endl;//sizeof(A) = 8,多重继承,D有两张虚表,也有两个执行项虚表的指针,所以为8
cin.get();
}
断点调试时内存模型: