C++ type casting

Type casting means converting from one type of data into another type.  c++ language supports two types of conversion.    (1)Implicit conversion  or implicit casting (2) explicit conversion or explicit casting. A very simple example of typecasting is given below. the example is an implicit type conversion. In below example, the conversion of data from float to int is done implicitly, we don’t need to specify any type operators. we will discuss later in this article what are these type operators?

using namespace std;
int main()

	float a = 3.0;
	int b = a; // implicit casting float type into integer type
	return 0;


The C++ language supports four typecast operators: explicit type casting

  • static_cast
  • reinterpret_cast
  • const_cast
  • dynamic_cast

Explicit type casting is used  generally for user defined data type with the help of type conversion operators mentioned above. The data type like int, float,double etc are fundamental data type. user defined data type is supported by c++ and struct, enumeration and class are the example of user defined data types. Now let us discuss static_cast, reinterpret_cast, const_cast and dynamic_cast with examples and requirements for “when to use these operators.


static_cast operator is used when data types involved in conversion is known or certain. the one disadvantage is that it does not perform run time type check. the static_cast can be used for fundamental data type conversions like int to float, enums to int, short to int and it is called standard conversion like in below example.

using namespace std;
int main()

	int a = 20;
	int b = 3;
	double out;

	out = static_cast<double> (a) / b; // typecast a to double
	return 0;

static_cast can also be used to convert base class pointer to derived class pointer and vice-versa. but converting base class pointer into derived class pointer is not safe as derived class can have fields and methods that are not in base class.  see blow example code:

using namespace std;

class Base {

class Derived: public Base {

int main()


	Base *b;
	Derived *d;
	d = static_cast<Derived*> (b); // Not safe

	b = static_cast<Base*> (d); // safe
	return 0;


This cast operator is used to convert one type into fundamentally different type such as  char* to int* and one class type into unrelated class type.   it guarantees that if we cast a pointer to a different type, and then reinterpret_cast it back to the original type, we get the original value. for example.

int* a;
void* b = reinterpret_cast<void*>(a);
int* c = reinterpret_cast<int*>(b);


a and c contain the same value, but the value of b is unspecified.


const_cast is used to cast away (remove) const or volatile attribute of c++ variables. here is example to convert a const to non-const.

Another example where we need to cast away the const attribute of cost object.


Before going into details of dynamic_cast operator let us discuss some related topics involved in dynamic casting.

  • upcasting

up-casting in c++ refers to convert  derived class pointer or reference to base class pointer or reference.

  • donwcasting

down casting refers to covert base class pointer or reference to derived class pointer or reference. try to compile blow sample code

The above code will not compile and give error:
error: invalid conversion from ‘A*’ to ‘B*’ [-fpermissive]
b_obj = a_obj; // downcasting

Why we are getting above error? we are getting above error because down casting in c++ is not allowed without explicit type cast.  now we have to do down casting like b_obj = (B *)a_obj; . this is explicit type casting.
So below modified code will compile successfully.

Ok, above code will compile successfully but can you guess what is problem with this down casting. because we are assigning base class pointer to derived class then it is expectation from the code that base class object might have some derived class properties which is invalid. derived class object might have some extra function in it which is not present in base class object. it shows that down casting is not safe in this case. c++ provides dynamic_cast operator for safe down casting. Let us discuss How? Lets try to compile below code. will this code compile?

The above code will not compile and give below error.

cannot dynamic_cast ‘a_obj’ (of type ‘class A*’) to type ‘class B*’ (source type is not polymorphic)
b_obj = dynamic_cast<B *>(a_obj); // downcasting

The above error is coming because dynamic_cast is only allowed if a class that is being down cast has at least one virtual method. virtual destructor is also allowed.

dynamic_cast gives bad_cast exception in case of reference of an object in case of failure and in case of dynamic casting of pointer of an object if it fails the pointer is assigned null. this is run time checking.

Reference exception example:

dynamic_cast with pointer example

 output:dynamic cast fail for aobj

Related Contents to follow