关于班级规模的问题。 [英] Question about sizeof a class.

查看:55
本文介绍了关于班级规模的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有以下课程:


class AAA {};


如果我这样做:

sizeof(AAA)

它给我1.为什么尺寸为1?


问候,


Ab。

Hi,
lets say I have the following class:

class AAA{};

if I do:
sizeof(AAA)
it gives me 1. Why is the size 1 ?

Regards,

Ab.

推荐答案

Abubakar写道:
Abubakar wrote:
你好,
让我说我有以下课程:

AAA级课程;}
如果我这样做:
sizeof(AAA)
它给了我1.为什么大小为1?
Hi,
lets say I have the following class:

class AAA{};

if I do:
sizeof(AAA)
it gives me 1. Why is the size 1 ?




因为C ++标准要求类的最小大小为1。


-cd



Because the minimum size of a class is required to be 1 by the C++ standard.

-cd


为什么标准说它应该是1?


Ab。


" Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>

写道消息新闻:uD ************** @ TK2MSFTNGP03.phx.gbl ...
And why does the standard says that it should be 1?

Ab.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uD**************@TK2MSFTNGP03.phx.gbl...
Abubakar写道:
Abubakar wrote:

让我说我有以下课程:

AAA级{};

如果我这样做:
sizeof(AAA)
它给了我1.为什么大小为1?
因为C ++
Hi,
lets say I have the following class:

class AAA{};

if I do:
sizeof(AAA)
it gives me 1. Why is the size 1 ?
Because the minimum size of a class is required to be 1 by the C++



标准要求类的最小大小为1。
-cd



Abubakar写道:
Abubakar wrote:
为什么标准说它应该是1?
And why does the standard says that it should be 1?




因为您应该始终能够获取对象的地址,并且2个区别对象的

地址应始终不同。这是

能够编写使用指针的通用代码(模板或其他)的基础。满足这些要求的唯一方法是始终拥有

sizeof(某物)> = 1


Arnaud

MVP - VC



Because you should always be able to take the address of an object, and the
addresses of 2 distincts objects should always be different. This is
fundamental to be able to write generic code (template or otherwise) that
use pointers. The only way to fullfill those requirements is to always have
sizeof(something) >=1

Arnaud
MVP - VC


这篇关于关于班级规模的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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