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

后端 未结 11 1503
逝去的感伤
逝去的感伤 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:39

    Yes, it's because of performance overhead. Virtual methods are called using virtual tables and indirection.

    In Java all methods are virtual and the overhead is also present. But, contrary to C++, the JIT compiler profiles the code during run-time and can in-line those methods which don't use this property. So, JVM knows where it's really needed and where not thus freeing You from making the decision on your own.

提交回复
热议问题