Binding in C++: Static and Dynamic Binding
Binding in C++ refers to the process of connecting a function call to its actual definition in memory.
This connection can happen at two different times: either during compile time or during runtime.
Based on when the connection happens, C++ provides two types of binding: static binding (also
known as early binding) and dynamic binding (also called late binding). Binding is a core part of how
functions are called in object-oriented programming, especially when dealing with polymorphism and
inheritance. Choosing the right type of binding depends on what the program needs - either speed
and simplicity or flexibility and runtime decision-making.
Static binding occurs at compile time, meaning the compiler knows exactly which function to call
before the program runs. This type of binding is used in normal functions, function overloading, and
compile-time polymorphism. It doesn't use the virtual keyword and usually works with regular objects
rather than pointers or references. Because everything is decided early, static binding is faster and
more efficient in execution. For example, if we create an Animal class with a sound() function and
call it using an object like Animal a; a.sound();, the compiler directly links the call to the sound()
function defined in Animal.
Static Binding Example:
#include <iostream>
using namespace std;
class Animal {
public:
void sound() {
cout << "Animal makes sound" << endl;
};
int main() {
Animal a;
a.sound(); // Static binding - resolved at compile time
return 0;
Dynamic binding, on the other hand, happens at runtime, where the function to be executed is
determined based on the actual type of the object. This is used in run-time polymorphism and
requires inheritance and virtual functions. The base class function must be marked with the virtual
keyword, and the function is typically called through a pointer or reference to the base class. For
example, if Animal is a base class and Dog is a derived class, using Animal* a = new Dog();
a->sound(); will result in the Dog's version of sound() being called. This allows programs to be more
flexible and adapt behavior during runtime, but it may be slightly slower due to the extra
decision-making.
Dynamic Binding Example:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes sound" << endl;
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
};
int main() {
Animal* a = new Dog();
a->sound(); // Dynamic binding - resolved at runtime
return 0;
Static Binding Dynamic Binding
Static binding is decided at compile time Dynamic binding is decided at runtime
Static binding uses normal functions Dynamic binding uses virtual functions
Static binding is faster Dynamic binding is slower
Static binding doesn't use `virtual` Dynamic binding requires `virtual` keyword
Static binding uses objects directly Dynamic binding uses base class pointers
Static binding supports compile-time polymorphismDynamic binding supports run-time polymorphism