我一直都在想這樣的事情,所以我決定拿出一個完整的答案。這是關於你可能期望的,它是可預測的(耶)!因此,根據以下信息,您應該能夠預測課程的大小。
使用Visual Studio Community 2017(版本15。2),在Release模式與所有的優化禁用和RTTI(Run-time Type Information)關閉,我已確定以下內容:
稍短答案:
首先:
- 在32(x86)位中,
<size of pointer> == 4
字節
- 在64(x64)位中,
<size of pointer> == 8
字節
- 當我說「虛類繼承」,我的意思是比如:
class ChildClass: virtual public ParentClass
現在,我的結論是:
- 空類爲1所字節
- 繼承的空類是仍然有1個字節
- 具有函數的空類仍然是1個字節(?!見注意下面用於解釋)
- 繼承與函數的空類的還是1字節
- 添加變量爲一個空類
<size of variable>
字節
- 繼承一類具有一個變量並添加另一變量是
<size of variables>
字節
- 繼承一個類並覆蓋其功能增加了一個虛函數表(在結論部提供進一步的解釋),並且是
<size of pointer>
字節
- 簡單地聲明一個函數VIRTUA升還增加了一個虛函數表,使它
<size of pointer>
字節
- 一個空類(有或沒有成員函數)的虛擬類繼承還增加了一個虛函數表,並且使類
<size of pointer>
字節的非空的
- 虛擬類繼承類還增加了一個虛函數表,但它變得有點複雜:增加
<size of pointer>
字節總數,包裝所有成員變量在儘可能多的<size of pointer>
字節爲增量是必要的覆蓋<total size of member variables>
- 是的,你沒有看錯。 ..(看我的猜測結論 ...)
注意我甚至嘗試過有函數()cout一些文本,創建一個類的實例,並調用函數;它不會改變函數類的大小(這不是優化)!我有點驚訝,但它確實有道理:成員函數不會改變,所以它們可以存儲在類本身的外部。
結論:
- 空類是1個字節,因爲這是它在存儲器中具有存在的最低要求。 一旦添加了數據或vtable數據,雖然開始從0字節開始計數。
- 添加一個(非虛擬)成員函數對大小沒有影響,因爲成員函數存儲在外部。
- 聲明一個成員函數是虛擬的(即使類沒有overridded!)或覆蓋在子類中的成員函數添加所謂的"vtable" or "virtual function table",允許Dynamic Dispatch(這是真正的超級真棒雖則使用,我強烈建議使用它)。該虛擬表消耗
<size of pointer>
字節,將<size of pointer>
字節添加到所述類。當然,這個vtable每個類只能存在一次(無論是否存在)。
- 添加一個成員變量通過成員變量增加類的大小,無論所述成員變量是在父母或子女類(父類仍是其自身的規模雖然,當然)。
- 虛擬類的繼承是變得複雜的唯一部分...所以...我想一個小實驗之後發生了什麼事情是:在類的大小實際上
<size of pointer>
字節是一次遞增,即使它不」噸需要消耗那麼多的內存,我猜,因爲它增加一個虛函數表「助手塊」每個<size of pointer>
字節的內存或東西...
龍答:
我用這個代碼確定了所有這些:
#include <iostream>
using namespace std;
class TestA
{
};
class TestB: public TestA
{
};
class TestC: virtual public TestA
{
};
class TestD
{
public:
int i;
};
class TestE: public TestD
{
public:
int j;
};
class TestF: virtual public TestD
{
public:
int j;
};
class TestG
{
public:
void function()
{
}
};
class TestH: public TestG
{
public:
void function()
{
}
};
class TestI: virtual public TestG
{
public:
void function()
{
}
};
class TestJ
{
public:
virtual void function()
{
}
};
class TestK: public TestJ
{
public:
void function() override
{
}
};
class TestL: virtual public TestJ
{
public:
void function() override
{
}
};
void main()
{
cout << "int:\t\t" << sizeof(int) << "\n";
cout << "TestA:\t\t" << sizeof(TestA) << "\t(empty class)\n";
cout << "TestB:\t\t" << sizeof(TestB) << "\t(inheriting empty class)\n";
cout << "TestC:\t\t" << sizeof(TestC) << "\t(virtual inheriting empty class)\n";
cout << "TestD:\t\t" << sizeof(TestD) << "\t(int class)\n";
cout << "TestE:\t\t" << sizeof(TestE) << "\t(inheriting int + int class)\n";
cout << "TestF:\t\t" << sizeof(TestF) << "\t(virtual inheriting int + int class)\n";
cout << "TestG:\t\t" << sizeof(TestG) << "\t(function class)\n";
cout << "TestH:\t\t" << sizeof(TestH) << "\t(inheriting function class)\n";
cout << "TestI:\t\t" << sizeof(TestI) << "\t(virtual inheriting function class)\n";
cout << "TestJ:\t\t" << sizeof(TestJ) << "\t(virtual function class)\n";
cout << "TestK:\t\t" << sizeof(TestK) << "\t(inheriting overriding function class)\n";
cout << "TestL:\t\t" << sizeof(TestL) << "\t(virtual inheriting overriding function class)\n";
cout << "\n";
system("pause");
}
輸出:
32(86)位:
int: 4
TestA: 1 (empty class)
TestB: 1 (inheriting empty class)
TestC: 4 (virtual inheriting empty class)
TestD: 4 (int class)
TestE: 8 (inheriting int + int class)
TestF: 12 (virtual inheriting int + int class)
TestG: 1 (function class)
TestH: 1 (inheriting function class)
TestI: 4 (virtual inheriting function class)
TestJ: 4 (virtual function class)
TestK: 4 (inheriting overriding function class)
TestL: 8 (virtual inheriting overriding function class)
64(64)位:
int: 4
TestA: 1 (empty class)
TestB: 1 (inheriting empty class)
TestC: 8 (virtual inheriting empty class)
TestD: 4 (int class)
TestE: 8 (inheriting int + int class)
TestF: 24 (virtual inheriting int + int class)
TestG: 1 (function class)
TestH: 1 (inheriting function class)
TestI: 8 (virtual inheriting function class)
TestJ: 8 (virtual function class)
TestK: 8 (inheriting overriding function class)
TestL: 16 (virtual inheriting overriding function class)
如果你想在多重繼承的信息,去圖它從你的混賬的自我! -.-
這個問題可能有點更具描述中包含「你如何確定在C++中對象的大小?」的標題。 – 2009-06-02 03:42:35
Here是關於這個問題的優秀書。 – 2009-06-02 03:26:53