What is early (static) and late (dynamic) binding in C++?

后端 未结 3 1427
陌清茗
陌清茗 2020-12-29 04:00

How does early and late binding look like in C++? Can you give example?

I read that function overloading is early binding and virtual functions is late binding. I re

3条回答
  •  时光取名叫无心
    2020-12-29 04:39

    These are true of all object-oriented languages, not just C++.

    Static, compile time binding is easy. There's no polymorphism involved. You know the type of the object when you write and compile and run the code. Sometimes a dog is just a dog.

    Dynamic, runtime binding is where polymorphism comes from.

    If you have a reference that's of parent type at compile type, you can assign a child type to it at runtime. The behavior of the reference will magically change to the appropriate type at runtime. A virtual table lookup will be done to let the runtime figure out what the dynamic type is.

提交回复
热议问题