码迷,mamicode.com
首页 > 编程语言 > 详细

Base类sizeof大小 -- C++

时间:2014-07-28 15:47:13      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:os   io   c++   size   ios   public   指针   test   

Base类只是一个指针,所以Base大小是4个字节。

但Base类中添加一个int a,Base的大小变成8字节。


#include <iostream>
#include <complex>

using namespace std;

class Base1
{
public:
	Base1(){cout << "Base1" << endl;}
	~Base1(){cout << "~Base1" << endl;}
	virtual void f(int){cout << "Base1 f(int)" << endl;}
	virtual void f(double){cout << "Base1 f(double)" << endl;}
	virtual void g(int i = 20){cout << "Base1 f(int)" << endl;}
	virtual void g2(int i = 10){cout << "Base1 f(int)" << endl;}
};

class Base2
{
public:
	Base2(){cout << "Base2" << endl;}
	~Base2(){cout << "~Base2" << endl;}
	virtual void f(int){cout << "Base2 f(int)" << endl;}
	virtual void f(double){cout << "Base2 f(double)" << endl;}
	virtual void g(int i = 20){cout << "Base2 f(int)" << endl;}
	virtual void g2(int i = 10){cout << "Base2 f(int)" << endl;}
	/* 4 字节 */
	int a;
};

class Base3
{
public:
	Base3(){ int a;int b;cout << "Base3" << endl;}
	~Base3(){cout << "~Base3" << endl;}
	virtual void f(int){cout << "Base3 f(int)" << endl;}
	virtual void f(double){cout << "Base3 f(double)" << endl;}
	virtual void g(int i = 20){cout << "Base3 f(int)" << endl;}
	virtual void g2(int i = 10){cout << "Base3 f(int)" << endl;}
	/* 4 字节 */
	int a;
	/* 4 字节 */
	int b;
};

int
main()
{
	Base1 a;
	/* 结果 4 字节 */
	cout << sizeof(Base1) << endl;

	Base2 b;
	/* 结果 8 字节 */
	cout << sizeof(Base2) << endl;
	
	Base3 c;
	/* 结果 12 字节 */
	cout << sizeof(Base3) << endl;

}
/*
[root@localhost test_class]# g++ class.cpp 
[root@localhost test_class]# ./a.out 
Base1
4
Base2
8
Base3
12
~Base3
~Base2
~Base1
*/


Base类sizeof大小 -- C++,布布扣,bubuko.com

Base类sizeof大小 -- C++

标签:os   io   c++   size   ios   public   指针   test   

原文地址:http://blog.csdn.net/cy_cai/article/details/38230089

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!