码迷,mamicode.com
首页 > 其他好文 > 详细

class的大小

时间:2018-05-24 18:09:28      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:turn   .com   get   nbsp   set   new   public   asi   sizeof   

在这里列出了空类,类有函数,值,没有函数的大小,注意Class也是默认字节对齐

#include <stdio.h>

class x
{

};

class cx
{
public:
	cx()
	{

	}
	~cx()
	{

	}
};

class cxHasVal
{
public:
	cxHasVal()
	{

	}
	~cxHasVal()
	{

	}
	int a;
	char y;
	
};

class cxHasValFunc
{
public:
	cxHasValFunc()
	{

	}
	~cxHasValFunc()
	{

	}
	int a;
	char y;
	int getASize()
	{
		return sizeof(a);
	}
	void setA()
	{
		a  = 5;
	}
};

int main()
{
	x emptyX;
	int emptySize = sizeof(x);
	printf("size of empty class is %d\n\n",emptySize);

	cx *classx = new cx();
	int clsizePt = sizeof(classx);
	int clsize = sizeof(cx);
	printf("size of class point is %d\n",clsizePt);
	printf("size of class is %d\n\n",clsize);

	cxHasVal *hasVal = new cxHasVal();
	hasVal->a = 12;
	int hasValSize = sizeof(hasVal);
	int rVal = sizeof(cxHasVal);
	printf("size of class Point has val is %d\n",hasValSize);
	printf("size of class has val is %d\n\n",rVal);

	cxHasValFunc *hasValFunc = new cxHasValFunc();
	int hasValFuncSize = sizeof(hasValFunc);
	int rValFunc = sizeof(cxHasValFunc);
	printf("size of class Point has val is %d\n",hasValFuncSize);
	printf("size of class has val Func is %d\n\n",rValFunc);

	return 0;
}

  运行结果:

技术分享图片

 

class的大小

标签:turn   .com   get   nbsp   set   new   public   asi   sizeof   

原文地址:https://www.cnblogs.com/anlia/p/9083652.html

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