6

Please take this query as a question of curiosity.

I just want to know that is there any limitation in the number of members a class can have in c++. Hope there will be some maximum allowed number since everything will be finite in a language I guess.

apaderno
  • 28,547
  • 16
  • 75
  • 90
CodeRider
  • 1,750
  • 6
  • 18
  • 34
  • 7
    Almost certain to be implementation defined, and the usual warning of "if you have to ask, you're doing something wrong" – Damien_The_Unbeliever Mar 22 '13 at 10:33
  • There is no limit defined in the specification - it would all depend on the platform and it's compiler. There is a minimum that each compiler should support, however. Its 16384. – Dariusz Mar 22 '13 at 10:35
  • 1
    @Damien_The_Unbeliever : Think this is not the right way to comment for a genuine question. Of course I accept your comment. But prefer answers than comments. – CodeRider Mar 22 '13 at 10:38
  • It's a comment because it doesn't answer your question as posed. But I was trying to point out that your question as posed isn't likely to have an answer. – Damien_The_Unbeliever Mar 22 '13 at 10:43
  • @Damien_The_Unbeliever: Saying "it's implementation defined" *is* an answer. Just because it isn't a number doesn't mean that it doesn't answer the question. – Nicol Bolas Mar 22 '13 at 10:44
  • you can find a detailed answer [here][1] [1]: http://stackoverflow.com/questions/3619340/maximum-number-of-fields-for-a-c-object –  Mar 22 '13 at 10:45
  • This question is ambiguous^Wclear, vague^Wprecise, incomplete^Wcomplete, overly broad^W^Wnarrow, or^Wand rhetorical^Wintended to be answered and cannot^Wcan be reasonably answered in its current form. – R. Martinho Fernandes Mar 22 '13 at 10:52

1 Answers1

8

That value is defined by each implementation. The C++ standard recommends some minimum supported quantities in Annex B:

— Data members in a single class [16 384].

[...]

— Direct and indirect base classes [16 384].

— Direct base classes for a single class [1 024].

— Members declared in a single class [4 096].

— Final overriding virtual functions in a class, accessible or not [16 384].

— Direct and indirect virtual bases of a class [1 024].

— Static members of a class [1 024].

The minimum for "members declared in a single class" is less than the one for "data members in a single class" because classes can inherit data members from their bases.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510