+ 1

What is late happening in late binding

Hi Assume that we have inheritance in class and it does not have virtual involved. So, this falls in early binding. In other words, object.functioncall will link function definition to base or derived class at compile time and function call is linked to set of instructions (due to function definition) at compile time only. No run time overhead. Runner just execute instructions linked at compile time. Correct? Now in case of virtual , it is late binding. But what is late? B class has virtual method so b, d1 and d2 all three class have vtable at compile time. At compile time only, we have pb = new d1 ; means pb knows at compile time that it has vptr of d1 class. Just link d1 method here at compile time. What is big deal here and why compiler doe not link Amy functions at compiler time? https://sololearn.com/compiler-playground/c9DNr7S8FSbc/?ref=app Question 2: virtual base class also include overhead when non virtual function is called ?

30th Oct 2024, 4:44 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 ответов
+ 3
Regarding Q1: As you already understand, using new to instantiate an object is telling the compiler to do late binding - allocating memory and assigning its pointer at runtime. Your binding choice ought to be deliberate as it affects performance characteristics. The compiler respects your binding choice so you get predictable behavior that you designed. As a rule of thumb, early binding pre-allocates the space in the executable, making it larger with longer load time, but instantly available at runtime. Late binding allows smaller executable for shorter load time, but added overhead in runtime. Past wisdom from Microsoft was that it is overall better to have a smaller executable, although back then persistent storage was slower. Do what you think is best in your environment.
30th Oct 2024, 8:46 PM
Brian
Brian - avatar
+ 2
Maybe late binding is about setting up the environment with the extra code to execute runtime polymorphism. https://www.simplilearn.com/tutorials/cpp-tutorial/polymorphism-in-cpp you can copy your codes in Compiler Explorer https://godbolt.org to see the difference in the assembly codes generated for early and late binded codes . Use late binding only if you need the flexibility. It adds complication and overhead to your code, as you noted. You also get late binding when you use function pointers https://www.learncpp.com/cpp-tutorial/early-binding-and-late-binding/ By using the keywords virtual and override, you are essentially instructing the compiler to set up the code so that the method bindings are carried out at run time, it will generate the extra codes to be able to do that. So you must be clear if you really need this extra complication.
31st Oct 2024, 1:21 AM
Bob_Li
Bob_Li - avatar
+ 1
Brian , may be you wanted to coney the other thing but came out wrongly. It's not new which enforces late binding. Agreed that new or reference is needed for late binding, but it's virtual which takes onus of late binding. Thanks and sorry if this was the same thing you wanted to coney. Bob_Li compiler explorer helps understand what is going on. Thanks for this. Basically , I am not arguing about what. Now, Concern is why. For static binding, class method call is linked directly. For late binding (due to virtual ) , it just create new pointer pointing to derived class. Then vptr comes into picture and base pointer moves to derived class vptr. This resolves call at run time. Why to keep it for run time ? We know base is pointing to derived so let's directly link derived class method at compile time only. Why overhead was introduced via vptr ?
31st Oct 2024, 9:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
The explanation I found was imagine you have an app which might need to have additional functionality extended by plugins. This cannot be determined when the code was compiled, but if you have runtime polymorphism in place, you can still extend your functionality without recompiling your code. I haven't seen a simple example of this, though
31st Oct 2024, 9:47 AM
Bob_Li
Bob_Li - avatar