static_cast and dynamic_cast in C++

In this tutorial, we will learn about static_cast and dynamic_cast in C++.

We know that, in C++, we can assign one variable to another of the same type. But what happens if the data type of both the variables is different. In such a case, implicit type conversion would take place. It means the conversion of one data type to another. This typecasting may or may not be implicitly supported in C++. If it is not supported, then we need to make use of casting methods available in C++. So, there are four explicit type casting methods available in C++. They are – static_cast, const_cast, reinterpret_cast and dynamic_cast.

In this tutorial, we will focus only on static_cast and dynamic_cast.

static_cast: C++

static_cast is the simplest one of all the cast. static_cast simply performs implicit conversions between types.
Let us see an example to understand this.

#include<iostream>
using namespace std;
int main()
{
  float f = 6.4;
  int i,j;
  i = f;      //implicit type conversion from float to int 
  j = static_cast<int>(f);
  cout<<i<<"\n"<<j;
  
}

OUTPUT:

6
6

Here, float data type is being converted to integer value.  So, basically line no 7 and 8 is doing the same thing. Then, what is the difference.
Suppose if the program is failing somewhere and we want to check where the implicit cast is being done, searching line 7 in the whole bunch of code is a tideous task. That is why, we use static_cast in such a case as it can be searched easily.
static_cast happens at compile time. In the program, it checks whether we can typecast ‘ f ‘, which is of float type into ‘a’, which is of integer type.

Now, let us see dynamic_cast.

Dynamic _cast: C++

In C++, a derived class reference/pointer can be treated as a base class pointer. This is called upcasting in C++. The opposite process, called downcasting, is not allowed in C++.  So, dynamic_cast is used to promote safe downcasting in C++. It is always performed with polymorphic classes having at least one virtual function inside the class.

Let us see an example.

#include<bits/stdc++.h>
using namespace std;
class base
{
  public:
    virtual void car()
    {
      cout<<"base";
      }
};
class derived:public base
{
  public:
    void gun()
    {
      cout<<"derived";
      }
};
int main()
{
  base *b = new derived;
  derived *d;
  d=b; 			 //base class pointer assigned to the derived class pointer
  d->gun();
}

The above code will show an error as base class pointer is getting assigned to the derived class pointer (downcasting). To make downcasting possible in C++, we need to rewrite the code using dynamic_cast.

#include<bits/stdc++.h>
using namespace std;
class base
{
  public:
    virtual void car()
    {
      cout<<"base";
      }
};
class derived:public base
{
  public:
    void gun()
    {
      cout<<"derived";
      }
};
int main()
{
  base *b = new derived;
  derived *d;
  d=dynamic_cast<derived*>(b);      //to promote downcasting
  cout<<d<"\n";
  d->gun();
}

OUTPUT:

0x161820
derived

This is how we can implement static_cast and dynamic_cast in C++.

Also read: PI Constant in C++

Leave a Reply

Your email address will not be published. Required fields are marked *