Difference between static and dynamic binding in C++

Google Translate Icon

This post will discuss the difference between static and dynamic binding (late and early binding) in C++.

Binding generally refers to a mapping of one thing to another. In the context of compiled languages, binding is the link between a function call and the function definition. When a function is called in C++, the program control binds to the memory address where that function is defined.

 
There are two types of binding in C++: static (or early) binding and dynamic (or late) binding. This post provides an overview of the differences between static and dynamic binding in C++.

  1. The static binding happens at the compile-time, and dynamic binding happens at the runtime. Hence, they are also called early and late binding, respectively.
  2. In static binding, the function definition and the function call are linked during the compile-time, whereas in dynamic binding, the function calls are not resolved until runtime. So, they are not bound until runtime.
  3. Static binding happens when all information needed to call a function is available at the compile-time. Dynamic binding happens when the compiler cannot determine all information needed for a function call at compile-time.
  4. Static binding can be achieved using the normal function calls, function overloading, and operator overloading, while dynamic binding can be achieved using the virtual functions.
  5. Since all information needed to call a function is available before runtime, static binding results in faster execution of a program. Unlike static binding, a function call is not resolved until runtime for later binding, resulting in somewhat slower execution of code.
  6. The major advantage of dynamic binding is that it is flexible since a single function can handle different types of objects at runtime. This significantly reduces the size of the codebase and also makes the source code more readable.

Example of Static Binding in C++:

Consider the following code, where the sum() function is overloaded to accept two and three integer arguments. Even though there are two functions with the same name inside the ComputeSum class, the function call sum() binds to the correct function depending on the parameters passed to those functions. This binding is done statically during compile time.

Download  Run Code

Output:

Sum is 30
Sum is 60

Example of Dynamic Binding in C++:

Consider the following code, where we have a base class B, and a derived class D. Base class B has a virtual function f(), which is overridden by a function in the derived class D, i.e., D::f() overrides B::f().

Now consider lines 30-34, where the decision as to which class’s function will be invoked depends on the dynamic type of the object pointed to by basePtr. This information can only be available at the runtime, and hence f() is subject to the dynamic binding.

Download  Run Code

Output:

The base class function is called.
The derived class function is called.

That’s all about the differences between static and dynamic binding in C++.

Rate this post

Average rating 4.74/5. Vote count: 164

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!