Dyamic vs Static Polymorphism in C++ : which is preferable?

前端 未结 4 1313
孤城傲影
孤城傲影 2020-12-25 14:44

I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic

4条回答
  •  长情又很酷
    2020-12-25 14:45

    A switch is nothing more than a sequence of jumps that -after optimized- becomes a jump to an address looked-up by a table. Exactly like a virtual function call is.

    If you have to jump depending on a type, you must first select the type. If the selection cannot be done at compile time (essentially because it depends on the input) you must always perform two operation: select & jump. The syntactic tool you use to select doesn't change the performance, since optimize the same.

    In fact you are reinventing the v-table.

提交回复
热议问题