What Is Binding in C++? Static (Early) and Dynamic (Late) Binding
Binding in C++ is the step that connects a function call to the actual function body that will run. It happens at compile time by default. It happens at run time only when the function is virtual and you call it through a pointer or a reference.
The two kinds have two names each.
- Static binding is also called early binding. The compiler decides.
- Dynamic binding is also called late binding. The program decides while running.
The rule is short. Static binding uses the declared type. Dynamic binding uses the real type of the object.
| Static binding | Dynamic binding | |
|---|---|---|
| Other name | Early binding | Late binding |
| Decided | At compile time | At run time |
| Applies to | Normal functions, overloads, non-virtual members | virtual members |
| Chosen by | The declared type of the pointer or reference | The actual type of the object |
| Cost | None | One pointer lookup, and no inlining |
| Enables | Speed | Polymorphism |
One example shows both at the same time.
#include <iostream> class Base { public: virtual ~Base() = default; virtual void show() { std::cout << "Base show\n"; } void name() { std::cout << "Base name\n"; } // not virtual }; class Derived : public Base { public: void show() override { std::cout << "Derived show\n"; } void name() { std::cout << "Derived name\n"; } }; int main() { Derived d; Base* p = &d; p->show(); // "Derived show" -> dynamic binding p->name(); // "Base name" -> static binding return 0; }
Both calls go through the same pointer. Only show is virtual, so only show reaches the derived version.
How dynamic binding works
A class with at least one virtual function gets a hidden table of function pointers. It is usually called the vtable. Every object of that class stores a hidden pointer to its class table.
A virtual call reads that pointer, looks up the slot, and jumps. That is one extra indirection. It also blocks inlining, which is the real cost in tight loops.
You get dynamic binding only when all three things are true.
- The function is declared
virtualin the base class. - The derived class defines a function with the same signature.
- You call it through a pointer or a reference to the base class.
The mistakes that break dynamic binding
Most binding bugs come from one of these four.
- Forgetting
virtual. Without it the base version runs, whatever the object really is. - Object slicing.
Base b = d;copies only the base part ofd. A call onbthen usesBase, becausebis aBase. - Virtual calls inside a constructor or destructor. During the base constructor the object is still a
Base, so the base version runs. - A missing virtual destructor.
delete basePtron a derived object is undefined behaviour unless the base destructor is virtual.
Write override on every derived function that is meant to replace a virtual one. If the signature does not match, the compiler reports it instead of silently creating a new function.
Binding in C, and std::bind
C has no virtual functions, so every call in C is statically bound. Late binding in C is written by hand. You put function pointers in a struct and set them at run time, which is the same idea as a vtable.
std::bind is a different topic that shares the word. It comes from <functional> and creates a callable with some arguments already fixed. Modern code usually writes a lambda instead, because a lambda is easier to read and easier to inline.
Data binding is not a C++ language feature
Some readers search for data binding. That term means keeping a value and a screen field in step. Standard C++ has no such feature.
Frameworks add it. Qt does it with signals and slots, and QML adds property bindings. In plain C++ you write the observer pattern yourself.
How to Prepare
- Be ready to define both terms in one sentence each. "Static binding is resolved by the compiler from the declared type" is the answer interviewers want.
- Know the cost, not only the behaviour. Say that a virtual call adds an indirection and prevents inlining.
- Practice the slicing question. It is the most common follow up after the basic definition.
- Connect it to design. Polymorphism is how most object oriented patterns work. Grokking the System Design Interview shows where that flexibility matters at system level.
- Drill the coding rounds too. Language trivia rarely decides an offer. Grokking the Coding Interview covers the problem patterns that do.
- Pick your interview language on purpose. Is C++ or Python better for AI? compares the two for speed and for readability.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72