What is the difference between static and dynamic binding?

前端 未结 6 653
予麋鹿
予麋鹿 2021-01-31 09:41

Binding times can be classified between two types: static and dynamic. What is the difference between static and dynamic binding?

Could you give a quick example of each

6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 10:19

    It depends when the binding happens: at compile time (static) or at runtime (dynamic). Static binding is used when you call a simple class method. When you start dealing with class hierarchies and virtual methods, compiler will start using so called VTABLEs. At that time the compiler doesn't know exactly what method to call and it has to wait until runtime to figure out the right method to be invoked (this is done through VTABLE). This is called dynamic binding.

    See Wikipedia article on Virtual tables for more details and references.

提交回复
热议问题