Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C++ Virtual Function Dispatch: Static and Dynamic Binding Mechanics

Tech 1

Consider the behavior of default arguments combined with virtual function overrides:

cpp class BaseClass { public: virtual void DisplayValue(int val = 10); };

void BaseClass::DisplayValue(int val) { std::cout << "BaseClass::DisplayValue, val = " << val << std::endl; }

class DerivedClass : public BaseClass { public: void DisplayValue(int val = 20) override; };

void DerivedClass::DisplayValue(int val) { std::cout << "DerivedClass::DisplayValue, val = " << val << std::endl; }

Execution scenarios:

cpp DerivedClass derivedObj; BaseClass* basePtr = (BaseClass*)&derivedObj;

basePtr->DisplayValue(); (*basePtr).DisplayValue(); derivedObj.DisplayValue(); ((BaseClass)derivedObj).DisplayValue();

Compiler implementation details reveal distinct binding mechanisms to these cals:

  1. Direct Object Invocation (derivedObj.DisplayValue()): The compiler resolves the target function address at compile time, resulting in static binding. The call is directly hardcoded to DerivedClass::DisplayValue() without traversing the vtable.

  2. Pointer and Dereferenced Pointer Invocation (basePtr->DisplayValue() and (*basePtr).DisplayValue()): Both invoke dynamic binding. The generated assembly retrieves the vtable pointer from the object's memory, locates the appropriate function slot, and performs an indirect call. The runtime type of the object dictates which implementation exceutes.

  3. Object Slicing via Cast (((BaseClass)derivedObj).DisplayValue()): This cast triggers object slicing. A completely new, temporary BaseClass instance is constructed using the base class copy constructor. Durring the execution of this copy constructor, the vptr of the new object is explicitly set to BaseClass::vftable, overwriting the original DerivedClass vptr. Consequently, the dynamic dispatch resolves to BaseClass::DisplayValue. Modifying members within this temporary sliced object affects only the temporary copy and leaves the original derivedObj unaltered.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.