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++?
C++03 standard, Annex B (implementation quantities):
Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.
The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With