Why not have all the functions as virtual in C++?

后端 未结 11 1495
逝去的感伤
逝去的感伤 2020-12-12 21:50

I know that virtual functions have an overhead of dereferencing to call a method. But I guess with modern architectural speed it is almost negligible.

  1. Is ther
11条回答
  •  青春惊慌失措
    2020-12-12 22:20

    But I guess with modern architectural speed it is almost negligible.

    This assumption is wrong, and, I guess, the main reason for this decision.

    Consider the case of inlining. C++’ sort function performs much faster than C’s otherwise similar qsort in some scenarios because it can inline its comparator argument, while C cannot (due to use of function pointers). In extreme cases, this can mean performance differences of as much as 700% (Scott Meyers, Effective STL).

    The same would be true for virtual functions. We’ve had similar discussions before; for instance, Is there any reason to use C++ instead of C, Perl, Python, etc?

提交回复
热议问题