What is the cost of inheritance?

前端 未结 7 1133
遥遥无期
遥遥无期 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 07:59

    I think we all guys have been programming too much as lone wolf .. We forget to take cost of maintenance + readability + extensions with regards to features. Here is my take

    Inheritance Cost++

    1. On smaller projects : time to develop increases. Easy to write all global sudoku code. Always has it taken more time for me, to write a class inheritance to do the right_thing.
    2. On smaller projects : Time to modify increases. It is not always easy to modify the existing code to confirm the existing interface.
    3. Time to design increases.
    4. Program is slightly inefficient due to multiple message passing, rather than exposed gut(I mean data members. :))
    5. Only for the virtual function calls via pointer to base class, there one single extra dereference.
    6. There is a small space penalty in terms of RTTI
    7. For sake of completeness I will add that, too many classes will add too many types and that is bound to increase your compilation time, no matter how small it might be.
    8. There is also cost of tracking multiple objects in terms of base class object and all for run-time system, which obviously mean a slight increase in code size + slight runtime performance penalty due to the exception delegation mechanism(whether you use it or not).
    9. You dont have to twist your arm unnaturally in a way of PIMPL, if all you want to do is to insulate users of your interface functions from getting recompiled. (This IS a HEAVY cost, trust me.)

    Inheritance Cost--

    1. As the program size grows larger than 1/2 thousand lines, it is more maintainable with inheritance. If you are the only one programming then you can easily push code without object upto 4k/5k lines.
    2. Cost of bug fixing reduces.
    3. You can easily extend the existing framework for more challenging tasks.

    I know I am being a little devils advocate, but I think we gotta be fair.

提交回复
热议问题