C++ 的最大字段数目的
这个答案指出在Java中,一个对象可以拥有的最大字段数是65536。C
++中的对象有这样的限制吗?
This answer states that in Java the maximum number of fields an object may have is 65536.
Is there any such limit imposed on an object in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++03 标准,附录 B(实施数量):
该列表包括
因此,没有定义的限制,但是应用限制的实现“应该”使限制至少与指示的值一样大。恐怕我不知道常见的实现实际上是做什么的,但如果他们不记录它,他们要么不合规,要么限制是“未知的”。我想“未知”通常意味着“在编译时可以容纳尽可能多的可用内存”。
顺便说一句,我不确定“类中的成员”和“类中声明的成员”之间有什么区别。我认为这意味着如果你的基类有10个数据成员,并且你的类声明了10个成员,那么你的类总共有20(或21)个数据成员(取决于基类子对象是否算作数据成员) )。
C++03 standard, Annex B (implementation quantities):
The list includes
So there's no defined limit, but an implementation which applies a limit "should" make the limit at least as big as the value indicated. I'm afraid I don't know what common implementations actually do, but if they don't document it they're either not compliant, or else the limit is "unknown". I guess that "unknown" generally means, "as many as we can fit in the available memory at compile time".
Btw, I'm not sure what the difference is between "members in a class" and "members declared in a class". I think it means that if your base class has 10 data members, and your class declares 10 members, then your class has 20 (or 21) data members in total (depending whether the base class sub-object counts as a data member or not).
我不相信 C++ 规范中有任何内容可以涵盖这一点,但我怀疑不同的编译器会有不同的限制。
I don't believe that there is anything in the C++ spec to cover this, but I suspect that different compilers will have different limits.
对象可以拥有的字段数量没有硬性限制,但我认为这高度依赖于平台和编译器。
另外,如果您在一个对象中使用甚至 100 个或更多字段,那么您的类的设计可能存在严重错误,因此不必担心限制,而应该担心 OOP 设计
There is no hard limit on the amount a fields an object can have, but saying that I imagine this is highly platform and compiler dependent.
Also there is probably something very wrong with the design of your class if you are using even 100 or more fields in an object, so shouldn't have to worry about limits instead worry about OOP design