您的位置:首页 > 产品设计 > UI/UE

计算class,struct,uion的sizeof(),struct与uion的区别

2013-08-29 11:08 429 查看
#include "stdafx.h"

#include "iostream"

#include "conio.h"

using namespace std;

//类中只有成员变量与虚函数参与计算

class A

{

public:

virtual void funa() ;//4个字节

virtual void funb() ;//4个字节

void fun() ;

static void fund() ;

static int si ;

A() ;

~A() ;

long a ;//4个字节,补齐8个字节

double d;//8个字节

private:

int i ;//4个字节

char c ;//补齐4个字节

};

//结构中字节不足要补齐

struct B

{

short a;//2个字节,补齐有4个字节

long b;//4个字节

double d ;//8个字节

long double dd ;//8个字节

char c;//1个字节,补齐8个字节

}bb;

//union中字节取最大字节并是最小字节的最小倍数,且赋值过的变量的值是由最后一次赋值决定

union UI

{

short a; //2个字节;

long b ;//4个字节

double d ;//8个字节

char c[9] ;//9个字节

};

union C

{

short a; //2个字节;

long b ;//4个字节

double d ;//8个字节

char c[5] ;//5个字节

};

/*



union C

{

short a; //2个字节;

long b ;//4个字节

//double d ;//8个字节

char c[5] ;//5个字节

};

*/

int _tmain(int argc, _TCHAR* argv[])

{

int num[5] = {1,2,3,4,5} ;

cout<<"sizeof(A)="<<sizeof(A)<<endl ;//输出32

cout<<"sizeof(B)="<<sizeof(B)<<endl ;//输出32

cout<<"sizeof(UI)="<<sizeof(UI)<<endl ;//输出16

cout<<"sizeof(C)="<<sizeof(C)<<endl ;//输出8

UI ui ;

ui.a = 11 ;

ui.b = 1 ;

cout<<"ui.a="<<ui.a<<",ui.b="<<ui.b <<endl;// 输出1 1

getch() ;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: