C++ Early vs Late Bindings

This is a note on early (static) binding vs late (dynamic) binding in C++.

Example of static vs dynamic binding in C++.

This post is heavily influenced by this other post (https://www.geeksforgeeks.org/virtual-function-cpp/. Another pretty good post (https://softwareengineering.stackexchange.com/questions/200115/what-is-early-and-late-binding/200123#200123 )

In the following code snippet, the Drived class inherits from thee base class.

#include
using namespace std;

class Base {
public:
void print(){cout << “in Base” << std::endl;}};

class Derived : public Base {
public:
void print(){cout << “in Derived” << std::endl; } };

int main(){

Base * base_obj = new Base();

Derived *   derived_obj = new Derived();

Base * base_derived = new Derived();

base_obj->print();
derived_obj->print();
base_derived->print();
}

However, since no virtual function is declared on “print()” in the base class, the types are resolved at compile time. As a result, “base_derived” will be  treated as a “base” class, despite of the fact that it is initialized with “new Drived”.

As a result, the program above will print the following

in Base

in Derived

in Base

To use late (dynamic) binding, we will need to declare the print function as virtual in the Base class as the example shown below.

class Base {
public:
  virtual void print(){cout << “in Base” << std::endl;}};
class Derived : public Base {
public:
void print(){cout << “in Derived” << std::endl; } };

int main(){

Base * base_obj = new Base();

Derived *   derived_obj = new Derived();

Base * base_derived = new Derived();

base_obj->print();
  derived_obj->print();
base_derived->print();
}

will output

in Base

in Derived

in Derived

 

Static vs Dynamic Binding in Java and Python

Similar static and dynamic binding can happen in Java as well. A good post is here (https://www.geeksforgeeks.org/static-vs-dynamic-binding-in-java/) . Java methods are “virtual” by default. You will need to explicitly mark them as static methods if you want to avoid dynamic binding.

Python has an even looser type system. Everything is dynamic (not really any compilation involved) and overriden by default.

Implementations of Static and Dynamic Binding

Virtual functions are declared in base class, and are automatically overridden in children / sub classes. A good post on Virtual Functions is here (https://www.geeksforgeeks.org/virtual-function-cpp/ ). Virtual functions has to be accessed through pointers.

So if we change the main function to the following

int main(){

  Base base_obj = Base();

  Derived derived_obj = Derived();

  Base base_derived = Derived();

  base_obj.print();

  derived_obj.print();

  base_derived.print();

}

Then dynamic dispatch will NOT happen even though “base_derived” is a Drived object and the print() method in Base is virtual. The following will be printed

in Base

in Derived

in Base

dynamic binding is implemented with vtable, along with dynamic dispatch.

A good post explaining this in more details is here ( https://softwareengineering.stackexchange.com/questions/200115/what-is-early-and-late-binding/200123#200123)

Other important concepts are vtables and dynamic dispatch . Vtable stores pointers of all the implementations of a virtual method. Every class has its own virtual table, and it stores virtual pointers to the most specific definition for each virtual function.

A good post explaining this in more details is here (https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s