私は C++ プログラミングが初めてで、仮想メンバー関数を含むクラスのサイズについて疑問があります。以下のコードを実行してください:
#include "stdafx.h"
#include <iostream>
using namespace std;
class BaseClass
{
private:
int a, b;
public:
BaseClass()
{
a = 10;
b = 20;
}
virtual int area()
{
return 0;
}
};
class DerivedClass1 : public BaseClass
{
int x;
public:
virtual void simple()
{
cout << "inside simple" << endl;
}
};
int main()
{
DerivedClass1 Obj;
cout << sizeof(Obj) << endl; // Displays 16 bytes
return 0;
}
上記のコードは、サイズを 16 バイトとして示しています。私によると、2 つの仮想ポインター (1 つは基本クラスから継承され、もう 1 つは独自の仮想関数のために派生クラス自体に追加されます) + 派生クラスの 3 つのデータ メンバーは 20 バイトに等しいため、20 バイトを表示する必要があります。だからそれがどのように私に説明してください....