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?
-
1The language standard probably stipulates a lower bound. But is this really going to be an issue in practice?– Oliver CharlesworthDec 5, 2013 at 16:28
-
2Chances are very high that if you are inheriting from more than 1 non-abstract base class, you have a severe design problem.– Zac HowlandDec 5, 2013 at 16:28
-
2Its 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 ThomasDec 5, 2013 at 16:30
-
1There 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 HowlandDec 5, 2013 at 16:40
2 Answers
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.
-
Could you please refer or attach to the document this information is stated? Thanks– dochoexJan 18, 2021 at 15:27
-
stackoverflow.com/questions/81656/… @dochoex. This non-official site is good for browsing in my opinion: eel.is/c++draft/#intro. Also cppreference has good stuff. Official github: github.com/cplusplus/draft– MatJan 18, 2021 at 16:52
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.