Casting is a conversion process to change data from one type to another. C++ has two types of conversions:
dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)
- Implicit conversion : Conversions are performed automatically by the compiler without the programmer's intervention. They are automatically performed when a value is copied to a compatible type.
For example :
short a=2000; int b; b=a;
Below table showing possible implicit type conversion.Some of these conversions may imply a loss of precision, which can signal compiler warning. This warning can prevent explicit conversions. Constructor or operator conversions in applicable conversions, which affect classes that include specific constructors or operator functions to perform conversions. For example:class A {}; class B { public: B (A a) {} }; A a; B b=a;
Here, an implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Therefore implicit conversions from A to B are allowed.
- Explicit conversion : Conversions are performed only when explicitly specified by the programmer.
short a=2000; int b; b = (int) a; // c-like cast notation b = int (a); // functional notation
Explicit conversion on class objects also possible using below method :// class type-casting #include <iostream> using namespace std; class Dummy { float i,j; }; class Addition { int x,y; public: Addition (int a, int b) { x=a; y=b; } int result() { return x+y;} }; int main () { Dummy d; Addition * padd; padd = (Addition*) &d; cout << padd->result(); return 0; }
In this case result will produce either a run-time error or a unexpected result. To control this type of issue C++ provides four type of casting operator : dynamic_cast, reinterpret_cast, static_cast and const_cast.
dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)
Lets discuss this one by one :
static_cast :
Format :
static_cast<type>(expression);
Example :
double d=3.14159265; int i = static_cast<int>(d);
The static_cast tells the compiler to attempt to convert between two different data types. It will convert between built-in types, even when there is a loss of precision. In addition, the static_cast operator can also convert between related pointer types. static_cast used when developer knows type casting is possible, because run time compiler not checking casting worked properly or not. Therefore, it is up to the programmer to ensure that the conversion is safe.
int* pToInt = &iVariable; float* pToFloat = &fVariable; float* pResult = static_cast<float*>(pToInt); //Will not work as the pointers are not related //(they are of different types).
static_cast also possible on class object by following way :
class Base {}; class Derived: public CBase {}; Base * a = new Base; Derived * b = static_cast<Derived*>(a);This would be valid, although b would point to an incomplete object of the class and could lead to run-time errors if de-referenced.
dynamic_cast:
Format :
dynamic_cast< type>(expression);
int iVariable = 10; float fVariable = dynamic_cast<float>(iVariable); /*This statement converts iVariable which is of type int to float. */
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class. dynamic_cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly.
dynamic_cast is always successful when we cast a class to one of its base classes:
class Base { }; class Derived: public Base { }; Base b; Base* pb; Derived d; Derived* pd; pb = dynamic_cast<base>(&d); // ok: derived-to-base pd = dynamic_cast<derived>(&b); // wrong: base-to-derived
The second expression in this piece of code would produce a compilation error since base to derived conversions are not allowed with dynamic_cast unless the base class is polymorphic.
// dynamic_cast #include <iostream> #include <exception> using namespace std; class Base { virtual void dummy() {} }; class Derived: public Base { int a; }; int main () { try { Base * pba = new Derived; Base * pbb = new Base; Derived * pd; pd = dynamic_cast<Derived*>(pba); if (pd==0) cout << "Null pointer on first type-cast" << endl; pd = dynamic_cast<Derived*>(pbb); if (pd==0) cout << "Null pointer on second type-cast" << endl; } catch (exception& e) {cout << "Exception: " << e.what();} return 0; }Output : Null pointer on second type-cast
dynamic_cast can also cast null pointers when casting performed between unrelated classes, and can also cast pointers of any type to void pointers (void*).
const_cast:
Format :
const_cast< type>(expression);
Example :
// const_cast #include <iostream> using namespace std; void print (char * str) { cout << str << endl; } int main () { const char * c = "sample text"; print ( const_cast<char *> (c) ); return 0; }
Output : sample text
Format :
const_cast does not cast between different types. Instead it changes the "constness" of the expression. It can make something const what was not const before, or it can make something volatile or changeable by getting rid of the const.
reinterpret_cast:
Format :
reinterpret_cast< type>(expression);
Example :
class A {}; class B {}; A * a = new A; B * b = reinterpret_cast<B*>(a);
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.
typeid allows to check the type of an expression:
a and b are of different types:
a is: int *
b is: int
typeid also used to keeping track of dynamic objects.When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:
a is: class Base *
b is: class Base *
*a is: class Base
*b is: class Derived
If the type typeid evaluates is a pointer preceded by the de-reference operator (*), and this pointer has a null value, typeid throws a bad_typeid exception.
So this all about type conversion, please add your valuable comments in comment section.
typeid :
typeid allows to check the type of an expression:
Format:
typeid (expression)
Example :
// typeid #include <iostream> #include <typeinfo> using namespace std; int main () { int * a,b; a=0; b=0; if (typeid(a) != typeid(b)) { cout << "a and b are of different types:\n"; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; } return 0; }Output:
a and b are of different types:
a is: int *
b is: int
typeid also used to keeping track of dynamic objects.When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:
// typeid, polymorphic class #include <iostream> #include <typeinfo> #include <exception> using namespace std; class Base { virtual void f(){} }; class Derived : public Base {}; int main () { try { Base* a = new Base; Base* b = new Derived; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; cout << "*a is: " << typeid(*a).name() << '\n'; cout << "*b is: " << typeid(*b).name() << '\n'; } catch (exception& e) { cout << "Exception: " << e.what() << endl; } return 0; }Output:
a is: class Base *
b is: class Base *
*a is: class Base
*b is: class Derived
If the type typeid evaluates is a pointer preceded by the de-reference operator (*), and this pointer has a null value, typeid throws a bad_typeid exception.
So this all about type conversion, please add your valuable comments in comment section.