What is the cost of inheritance?

前端 未结 7 1160
遥遥无期
遥遥无期 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:52

    Inheriting from a class costs nothing at runtime.

    The class instances will of course take up more memory if you have variables in the base class, but no more than if they were in the derived class directly and you didn't inherit from anything.

    This does not take into account virtual methods, which do incur a small runtime cost.

    tl;dr: You shouldn't be worrying about it.

提交回复
热议问题