views:

154

answers:

3

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++?

+1  A: 

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.

Stephen Doyle
+1  A: 

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

Tristan
+14  A: 

C++03 standard, Annex B (implementation quantities):

  1. 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.

  2. 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

  • Size of an object [262 144].
  • Data members in a single class, structure, or union [16 384].
  • Members declared in a single class [4 096].

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).

Steve Jessop