What is the cost of inheritance?

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

    If you need the functionality of FooBase in Foo, either you can derive or use composition. Deriving has the cost of the vtable, and FooBase has the cost of a pointer to a FooBase, the FooBase, and the FooBase's vtable. So they are (roughly) similar and you shouldn't have to worry about the cost of inheritance.

提交回复
热议问题