What is the cost of inheritance?

前端 未结 7 1132
遥遥无期
遥遥无期 2020-12-06 07:11

This is a pretty basic question but I\'m still unsure:

If I have a class that will be instantiated millions of times -- is it advisable not to derive it from some o

相关标签:
7条回答
  • 2020-12-06 08:16

    The fact is that if you are in doubt of whether you should inherit or not, the answer is that you should not. Inheritance is the second most coupling relationship in the language.

    As of the performance difference, there should be almost no difference in most cases, unless you start using multiple inheritance, where if one of the bases has virtual functions, the dispatch will have an additional (minimal, negligible) cost if the base subobject is not aligned with the final overrider as the compiler will add a thunk to adjust the this pointer.

    0 讨论(0)
提交回复
热议问题