13

What is the limit of multiple inheritance in C++? i.e, how many classes can a class inherit from? Is it implementation dependent or is there a constraint placed on the number of classes you can inherit from in multiple inheritance?

Tom Thomas
  • 619
  • 2
  • 8
  • 24
  • 1
    The language standard probably stipulates a lower bound. But is this really going to be an issue in practice? – Oliver Charlesworth Dec 05 '13 at 16:28
  • 2
    Chances are very high that if you are inheriting from more than 1 non-abstract base class, you have a severe design problem. – Zac Howland Dec 05 '13 at 16:28
  • 2
    Its not about a design flaw or a case of judgement. I don't even work with C++. A curious kid happens to ask me this and I don't really have an answer. So somebody tell me.. – Tom Thomas Dec 05 '13 at 16:30
  • 1
    There is what the standard allows (both Mat and M.M. have stated that), but then there is what is practical. Multiple inheritance of non-abstract base classes is messy, and 95% of the time should be avoided. – Zac Howland Dec 05 '13 at 16:40

2 Answers2

16

It's implementation defined. C++11 gives recommended minimums in the Implementation quantities section of the standard:

— Direct and indirect base classes [16 384].
— Direct base classes for a single class [1 024].
[...]
— Direct and indirect virtual bases of a class [1 024].

I'd say that's pretty generous.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Could you please refer or attach to the document this information is stated? Thanks – dochoex Jan 18 '21 at 15:27
  • https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents @dochoex. This non-official site is good for browsing in my opinion: http://eel.is/c++draft/#intro. Also cppreference has good stuff. Official github: https://github.com/cplusplus/draft – Mat Jan 18 '21 at 16:52
5

Per §10.1:

1 A class can be derived from any number of base classes. [Note: The use of more than one direct base class is often called multiple inheritance. —end note ]

Everything else depends on compiler's implementation and limitations.

masoud
  • 55,379
  • 16
  • 141
  • 208