为什么C ++中的一个空类的大小不是零? [英] Why is the size of an empty class in C++ not zero?
本文介绍了为什么C ++中的一个空类的大小不是零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下输出 1
?
#include <iostream>
class Test
{
};
int main()
{
std::cout << sizeof(Test);
return 0;
}
推荐答案
标准不允许对象(及其类),因为这将使得两个不同对象具有相同的存储器地址成为可能。这就是为什么即使空的类必须有(至少)1的大小。
The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.
这篇关于为什么C ++中的一个空类的大小不是零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文