Introduction

In C++, binding refers to the process of associating a function call with the actual function code that should be executed. There are two types of binding in C++: static and dynamic binding (also known as late binding or runtime binding). The main difference between them lies in when the association between the function call and the function implementation is determined. Static binding is resolved at compile-time based on the declared type, while dynamic binding is resolved at runtime based on the actual type of the object being referred to, and it requires the use of virtual functions.

Static binding

In C++, static binding (also known as early binding or compile-time binding) refers to the resolution of a function call at compile-time rather than runtime. It occurs when the compiler knows exactly which function to call based on the static (declared) type of the object being used.

Static binding occurs in the following situations:

  1. Function Overloading: When a class has multiple member functions with the same name but different parameter lists, the compiler determines which function to call based on the static type of the arguments.
  2. Non-virtual Functions: When a member function is not declared as virtual in the base class and is called using a pointer or reference to the base class, the compiler resolves the function call based on the static type of the pointer/reference.

Here’s a simple example to illustrate static binding:

#include <iostream>

class Base {
public:
    void display() {
        std::cout << "Base class display" << std::endl;
    }
};

class Derived : public Base {
public:
    void display() {
        std::cout << "Derived class display" << std::endl;
    }
};

int main() {
    Base baseObj;
    Derived derivedObj;

    Base* ptr = &derivedObj; // Pointer to a Derived object (Upcasting)

    baseObj.display(); // Static binding calls Base::display()
    derivedObj.display(); // Static binding calls Derived::display()
    ptr->display(); // Static binding calls Base::display() (the static type of ptr is Base*)
    
    return 0;
}

Dynamic Binding

Dynamic binding, also known as late binding or runtime polymorphism, is an important concept in object-oriented programming, including C++. It allows you to write more flexible and extensible code by enabling the selection of the appropriate function or method to be executed at runtime based on the actual object type rather than the type of the reference or pointer used to access it.

Dynamic binding allows you to write generic code that can work with different derived classes without knowing their exact types at compile time, promoting flexibility and code reuse.

In C++, dynamic binding is achieved through the use of virtual functions and inheritance. Here’s how it works:

  1. Virtual Functions: A virtual function is a member function declared in a base class and marked with the virtual keyword. When a function is declared virtual, it indicates to the compiler that the function should participate in dynamic binding. Virtual functions are intended to be overridden in derived classes, providing different implementations for the same function signature.
  2. Inheritance: When a derived class inherits from a base class, it can override the virtual function with its own implementation. The derived class redefines the virtual function using the override keyword, indicating that it’s intended to override a virtual function from the base class.

Here’s a simple example to illustrate dynamic binding:

#include <iostream>

class Base {
public:
    virtual void display() {
        std::cout << "Base class display" << std::endl;
    }
};

class Derived : public Base {
public:
    void display() override {
        std::cout << "Derived class display" << std::endl;
    }
};

int main() {
    Base baseObj;
    Derived derivedObj;

    Base* ptr = &derivedObj; // Pointer to a Derived object (Upcasting)

    baseObj.display(); // Static binding calls Base::display()
    derivedObj.display(); // Static binding calls Derived::display()
    ptr->display(); // Dynamic binding calls Derived::display() (the dynamic type of ptr is Derived*)

    return 0;
}

Difference between static binding and dynamic binding

STATIC BINDING DYNAMIC BINDING
It is associated with the compile-time polymorphism which is also called early binding.It is associated with the run-time polymorphism which is also called late binding.
It is achieved through the function overloading and function overriding.It is achieved by using virtual functions and interfaces, which allow dynamic method resolution during runtime.
Static binding is generally slightly faster because method resolution is done at compile-time.Dynamic binding is generally slightly slower because it involves an extra run-time lookup.
It determines which function to call at compile-time based on the static type of the object reference or pointer.It determines it at run-time based on the actual object type during execution.
Static binding errors, such as calling non-existing functions, are detected at compile-time, which provides better feedback during development. Dynamic binding errors are detected at run-time, which may lead to unexpected behavior during execution.

Conclusion

To summarize, static binding in C++ is determined at compile-time based on the declared type of the object, while dynamic binding is determined at runtime based on the actual type of the object. Dynamic binding is achieved by using virtual functions and is essential for achieving polymorphism in C++.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.