什么是空类的大小和在C ++中的联合,结构和类之间的区别? [英] what is size of empty class and difference between union, structure and class in c++ ?

查看:113
本文介绍了什么是空类的大小和在C ++中的联合,结构和类之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是空类的大小以及在c ++中的union,结构和类之间的区别?

what is size of empty class and difference between union, structure and class in c++ ?

我的想法:

如果没有静态成员,他们应该是相同的,因为所有成员都分配堆栈。

if no static members in them, they should be same because all members are allocated on stack.

如果它们都是空的,它们是相同的。

If they are all empty, they are same.

如果他们有静态成员,

if they have static members, it depends the relative location of the members inside them.

向右?

感谢

推荐答案

C ++标准标准规定空类应为非零

通常在大多数系统上为 1 字节。

C++ Standard standard specify's that the size of an Empty class should be Non-Zero.
Usually, it is 1 byte on most systems.

在Bjarne Stroustrup的话中,大小不为零确保两个不同对象的地址不同。

In Bjarne Stroustrup's words, the size is non-zero "To ensure that the addresses of two different objects will be different."

在大多数系统上,大小是 1 ,因为对齐规则没有关系,因为类名的条目在符号表中只是

The size is 1 on most systems because the alignment rules don't matter as the entry of the class name is made in symbol table just to obtain an unique address.

对于Standerdese粉丝:

C ++ 03标准第9节:类,第2节:

For Standerdese fans:
C++03 Standard Section 9: Classes, Para 2:


类类型的完整对象和成员子对象应具有非零大小。
94)

Complete objects and member subobjects of class type shall have nonzero size. 94)

这篇关于什么是空类的大小和在C ++中的联合,结构和类之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆