随笔-0  评论-0  文章-24  trackbacks-0

一个空类:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9         ;
10
11}
;
12
13int main(void)
14
15{
16
17         cout << sizeof (Test) << endl;
18
19         return 0;
20
21}

22

 


空类继承一个空类:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Base
 6
 7{
 8
 9         ;
10
11}
;
12
13class Derived:public Base
14
15{
16
17         ;
18
19}
;
20
21int main(void)
22
23{
24
25         cout << sizeof(Derived) << endl;
26
27         return 0;
28
29}

30
31

 


空类继承一个非空类:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Base
 6
 7{
 8
 9private:
10
11         int i;
12
13         char c;
14
15         double d;
16
17}
;
18
19class Derived:public Base
20
21{
22
23         ;
24
25}
;
26
27int main(void)
28
29{
30
31         cout << sizeof(Base) << endl;
32
33         cout << sizeof(Derived) << endl;
34
35         return 0;
36
37}

38
39

 



总结:一个空类占的大小是1,一个空类要是继承一个非空类大小应该是非空类的大小,继承一个空类大小还是1.





 

现在,我们添加成员函数

一个成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13}
;
14
15int Test::show(int a)
16
17{
18
19         cout << a << endl;
20
21         return a;
22
23}

24
25int main(void)
26
27{
28
29         cout << sizeof (Test) << endl;
30
31         return 0;
32
33}

34
35

 

 

一个static成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         static int show(int);
12
13}
;
14
15int Test::show(int a)
16
17{
18
19         cout << a << endl;
20
21         return a;
22
23}

24
25int main(void)
26
27{
28
29         cout << sizeof (Test) << endl;
30
31         return 0;
32
33}

34
35

 


两个成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15}
;
16
17int Test::show(int i)
18
19{
20
21         cout << i << endl;
22
23         return i;
24
25}

26
27double Test::display(double d)
28
29{
30
31         cout << d << endl;
32
33         return d;
34
35}

36
37int main(void)
38
39{
40
41         cout << sizeof (Test) << endl;
42
43         return 0;
44
45}

46
47

 


两个
static成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         static int show(int);
12
13         static double display(double);
14
15}
;
16
17int Test::show(int i)
18
19{
20
21         cout << i << endl;
22
23         return i;
24
25}

26
27double Test::display(double d)
28
29{
30
31         cout << d << endl;
32
33         return d;
34
35}

36
37int main(void)
38
39{
40
41         cout << sizeof (Test) << endl;
42
43         return 0;
44
45}

46
47

 


三个成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17}
;
18
19int Test::show(int i)
20
21{
22
23         cout << i << endl;
24
25         return i;
26
27}

28
29double Test::display(double d)
30
31{
32
33         cout << d << endl;
34
35         return d;
36
37}

38
39char Test::transfer(char c)
40
41{
42
43         cout << c << endl;
44
45         return c;
46
47}

48
49int main(void)
50
51{
52
53         cout << sizeof (Test) << endl;
54
55         return 0;
56
57}

58
59

 


三个static成员函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         static int show(int);
12
13         static double display(double);
14
15         static char transfer(char);
16
17}
;
18
19int Test::show(int i)
20
21{
22
23         cout << i << endl;
24
25         return i;
26
27}

28
29double Test::display(double d)
30
31{
32
33         cout << d << endl;
34
35         return d;
36
37}

38
39char Test::transfer(char c)
40
41{
42
43         cout << c << endl;
44
45         return c;
46
47}

48
49int main(void)
50
51{
52
53         cout << sizeof (Test) << endl;
54
55         return 0;
56
57}

58
59

 


总结:C++编译系统正是这样做的,因此每个对象所占用的存储空间只是该对象的数据部分所占用的存储空间,而不包括函数代码所占用的存储空间。注意:virtual成员函数除外。






现在,我们添加成员变量:

一个成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         int a;
20
21}
;
22
23int Test::show(int i)
24
25{
26
27         cout << i << endl;
28
29         return i;
30
31}

32
33double Test::display(double d)
34
35{
36
37         cout << d << endl;
38
39         return d;
40
41}

42
43char Test::transfer(char c)
44
45{
46
47         cout << c << endl;
48
49         return c;
50
51}

52
53int main(void)
54
55{
56
57         cout << sizeof (Test) << endl;
58
59         return 0;
60
61}

62
63

 


一个static成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         static int a;
20
21}
;
22
23int Test::a = 1;
24
25int Test::show(int i)
26
27{
28
29         cout << i << endl;
30
31         return i;
32
33}

34
35double Test::display(double d)
36
37{
38
39         cout << d << endl;
40
41         return d;
42
43}

44
45char Test::transfer(char c)
46
47{
48
49         cout << c << endl;
50
51         return c;
52
53}

54
55int main(void)
56
57{
58
59         cout << sizeof (Test) << endl;
60
61         return 0;
62
63}

64
65

 


两个成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         int a;
20
21         char c;
22
23}
;
24
25int Test::show(int i)
26
27{
28
29         cout << i << endl;
30
31         return i;
32
33}

34
35double Test::display(double d)
36
37{
38
39         cout << d << endl;
40
41         return d;
42
43}

44
45char Test::transfer(char c)
46
47{
48
49         cout << c << endl;
50
51         return c;
52
53}

54
55int main(void)
56
57{
58
59         cout << sizeof (Test) << endl;
60
61         return 0;
62
63}

64
65

 


两个static成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         static int a;
20
21         static char c;
22
23}
;
24
25int Test::a = 1;
26
27char Test::c = 0;
28
29int Test::show(int i)
30
31{
32
33         cout << i << endl;
34
35         return i;
36
37}

38
39double Test::display(double d)
40
41{
42
43         cout << d << endl;
44
45         return d;
46
47}

48
49char Test::transfer(char c)
50
51{
52
53         cout << c << endl;
54
55         return c;
56
57}

58
59int main(void)
60
61{
62
63         cout << sizeof (Test) << endl;
64
65         return 0;
66
67}

68
69

 


三个成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         int a;
20
21         char c;
22
23         double d;
24
25}
;
26
27int Test::show(int i)
28
29{
30
31         cout << i << endl;
32
33         return i;
34
35}

36
37double Test::display(double d)
38
39{
40
41         cout << d << endl;
42
43         return d;
44
45}

46
47char Test::transfer(char c)
48
49{
50
51         cout << c << endl;
52
53         return c;
54
55}

56
57int main(void)
58
59{
60
61         cout << sizeof (Test) << endl;
62
63         return 0;
64
65}

66
67

 


三个static成员变量:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         static int a;
20
21         static char c;
22
23         static double d;
24
25}
;
26
27int Test::a = 1;
28
29char Test::c = 0;
30
31double Test::d = 0.0;
32
33int Test::show(int i)
34
35{
36
37         cout << i << endl;
38
39         return i;
40
41}

42
43double Test::display(double d)
44
45{
46
47         cout << d << endl;
48
49         return d;
50
51}

52
53char Test::transfer(char c)
54
55{
56
57         cout << c << endl;
58
59         return c;
60
61}

62
63int main(void)
64
65{
66
67         cout << sizeof (Test) << endl;
68
69         return 0;
70
71}

72
73

 


总结:static成员变量时不占用内存空间的(当然,static成员函数也不占用空间)。非static成员变量占用内存空间,大家可以看到的是,有内存对齐的情况。






内存对齐:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17private:
18
19         static int a;
20
21         char c;
22
23         int i;
24
25         char c1;
26
27         double d;
28
29}
;
30
31int Test::a = 1;
32
33int Test::show(int i)
34
35{
36
37         cout << i << endl;
38
39         return i;
40
41}

42
43double Test::display(double d)
44
45{
46
47         cout << d << endl;
48
49         return d;
50
51}

52
53char Test::transfer(char c)
54
55{
56
57         cout << c << endl;
58
59         return c;
60
61}

62
63int main(void)
64
65{
66
67         cout << sizeof (Test) << endl;
68
69         return 0;
70
71}

72
73

 



VC++6.0默认对齐为8字节:


当然,我们可以改变它:

 1#include <iostream>
 2
 3using namespace std;
 4
 5#pragma pack(push) //保存对齐状态
 6
 7#pragma pack(1)    //设定为1字节对齐
 8
 9class Test
10
11{
12
13public:
14
15         int show(int);
16
17         double display(double);
18
19         char transfer(char);
20
21private:
22
23         static int a;
24
25         char c;
26
27         int i;
28
29         char c1;
30
31         double d;
32
33}
;
34
35#pragma pack(pop) //恢复对齐状态
36
37int Test::a = 1;
38
39int Test::show(int i)
40
41{
42
43         cout << i << endl;
44
45         return i;
46
47}

48
49double Test::display(double d)
50
51{
52
53         cout << d << endl;
54
55         return d;
56
57}

58
59char Test::transfer(char c)
60
61{
62
63         cout << c << endl;
64
65         return c;
66
67}

68
69int main(void)
70
71{
72
73         cout << sizeof (Test) << endl;
74
75         return 0;
76
77}

78
79

 


现在,我们添加虚函数:

一个虚函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17         virtual void p2p();
18
19}
;
20
21int Test::show(int i)
22
23{
24
25         cout << i << endl;
26
27         return i;
28
29}

30
31double Test::display(double d)
32
33{
34
35         cout << d << endl;
36
37         return d;
38
39}

40
41char Test::transfer(char c)
42
43{
44
45         cout << c << endl;
46
47         return c;
48
49}

50
51int main(void)
52
53{
54
55         cout << sizeof (Test) << endl;
56
57         return 0;
58
59}

60
61

 


两个虚函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17         virtual void p2p();
18
19         virtual void pps();
20
21}
;
22
23int Test::show(int i)
24
25{
26
27         cout << i << endl;
28
29         return i;
30
31}

32
33double Test::display(double d)
34
35{
36
37         cout << d << endl;
38
39         return d;
40
41}

42
43char Test::transfer(char c)
44
45{
46
47         cout << c << endl;
48
49         return c;
50
51}

52
53int main(void)
54
55{
56
57         cout << sizeof (Test) << endl;
58
59         return 0;
60
61}

62
63

 


三个虚函数:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Test
 6
 7{
 8
 9public:
10
11         int show(int);
12
13         double display(double);
14
15         char transfer(char);
16
17         virtual void p2p();
18
19         virtual void pps();
20
21         virtual void ppp();
22
23}
;
24
25int Test::show(int i)
26
27{
28
29         cout << i << endl;
30
31         return i;
32
33}

34
35double Test::display(double d)
36
37{
38
39         cout << d << endl;
40
41         return d;
42
43}

44
45char Test::transfer(char c)
46
47{
48
49         cout << c << endl;
50
51         return c;
52
53}

54
55int main(void)
56
57{
58
59         cout << sizeof (Test) << endl;
60
61         return 0;
62
63}

64
65

 


虚函数:每个类有一个vtable,叫做虚表。每个对象有一个vptr,叫做虚指针,指向类的虚表。如果在32位机器上,因为它是一个指针,所以,大小为4.

单继承:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Base
 6
 7{
 8
 9public:
10
11         virtual void pps();
12
13}
;
14
15class Derived:public Base
16
17{
18
19public:
20
21         virtual void p2p();
22
23         virtual void p2sp();
24
25}
;
26
27int main(void)
28
29{
30
31         cout << sizeof (Derived) << endl;
32
33         return 0;
34
35}

36
37

 


多继承:

 1#include <iostream>
 2
 3using namespace std;
 4
 5class Base
 6
 7{
 8
 9public:
10
11         virtual void pps();
12
13}
;
14
15class Base1
16
17{
18
19public:
20
21         virtual void ppp();
22
23}
;
24
25class Base2
26
27{
28
29public:
30
31         virtual void pp();
32
33}
;
34
35class Derived:public Base,public Base1,public Base2
36
37{
38
39public:
40
41         virtual void p2p();
42
43         virtual void p2sp();
44
45}
;
46
47int main(void)
48
49{
50
51         cout << sizeof (Derived) << endl;
52
53         return 0;
54
55}

56
57

 

总结:单继承的话,子类的虚表覆盖父类的虚表,只有一个虚表。所以,在32位机器上,大小为4.多继承的话,子类继承每个父类的虚表,所以,为12。
















 


posted on 2012-10-23 11:04 zhuxin 阅读(250) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理