本文介绍了C ++如何计算对象的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 如果我有以下内容: class Foo { void something(void){; } }; 然后我说: Foo f; cout<< sizeof f<< endl; 它告诉我f的大小是1个字节。我想知道C ++如何知道f的大小是一个字节。是因为隐含的''this'指针? 谢谢!Hi all,If I have the following:class Foo{void something(void){;}};and then later I say:Foo f;cout<<sizeof f<<endl;It tells me the size of f is 1 byte. I wonder how does C++ know thesize of f is one byte. Is it because the implicit ''this'' pointer?Thanks!推荐答案 对象的大小是特定于实现的。在你的情况下,它显示为 如果你的实现用一个字节填充你的类什么, 大概是因为一个0字节的对象不能有一个地址。 - PeteThe size of an object is implementation-specific. In your case it appears asif your implementation padded your class of nothing with one byte,presumably because an object of 0 bytes cannot have an address.- Pete 对象的大小是特定于实现的。在你的情况下,它显示为如果你的实现用一个字节填充你的类,大概是因为一个0字节的对象不能有一个地址。 The size of an object is implementation-specific. In your case it appears as if your implementation padded your class of nothing with one byte, presumably because an object of 0 bytes cannot have an address. 这不是因为它没有地址,而是因为可以有 没有大小为0的对象数组,或者如果有,每个元素 与其他人无法区分。 基类子对象的大小为0,其地址相同 as包含对象的地址。找出它们的大小为0的唯一方法是向类中添加一个空基,并比较该类之前和之后的实例的大小 。 VictorIt''s not because it cannot have an address, it''s because there can beno array of an objects of size 0, or if there would be, each elementwould be indistinguishable from the others.Base class subobjects can be of size 0, and their address is the sameas the address of the containing object. The only way to figure outthat they have size 0 is to add an empty base to the class and comparethe sizes of an instance of that class before and after.Victor 这不是因为它没有地址,而是因为可能没有大小为0的对象数组,或者如果有的话,每个元素都会与其他人无法区分。 It''s not because it cannot have an address, it''s because there can be no array of an objects of size 0, or if there would be, each element would be indistinguishable from the others. 是的,但字面上怎么没有地址呢?而C ++中的AFAIK 非注册对象需要有一个地址... - Pete 基类子对象可以是0的大小,它们的地址与包含对象的地址相同。确定它们的大小为0的唯一方法是在类中添加一个空基,并比较该类之前和之后的实例的大小。 VictorYes, but how can literally nothing have an address? And AFAIK in C++non-register objects are required to have an address...- Pete Base class subobjects can be of size 0, and their address is the same as the address of the containing object. The only way to figure out that they have size 0 is to add an empty base to the class and compare the sizes of an instance of that class before and after. Victor 这篇关于C ++如何计算对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 13:24