C/C++中的空类及抽象类大小

代码:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 struct A{
 6 };
 7 
 8 struct B{
 9     int i;
10 };
11 
12 class C:B{
13     virtual void func1()=0;
14 };
15 
16 int main(){
17 
18     cout<<sizeof(A)<<endl;
19     cout<<sizeof(B)<<endl;
20     cout<<sizeof(C)<<endl;
21     return 0;
22 }

输出:

1
4
16

 分析:

空类的大小为1,纯虚函数占8个字节。

posted @ 2016-05-10 18:38  hu983  阅读(472)  评论(0编辑  收藏  举报