Monday, June 3, 2013

TypeCasting in C++


Typecasting is the concept of converting the value of one type into another type.

 Implicit conversion or Automatic typecasting


Compiler automatically converts one type into another type. If the compiler converts a type it will normally give a warning.

Example  warning: conversion from ‘double’ to ‘int’, possible loss of data

Explicit conversion

#include <iostream>
using namespace std;

int main()
{
 int a;
 double b=2.55;
 a = b;       //Implicit by compiler with warning
 cout << a << endl;  
 a = (int)b; //Explicit C Style
 cout << a << endl;
 a = int(b); //Explicit C++ Style
 cout << a << endl; 
} 
O/P : All print 2 
 

Type Casting in C++

 The C++ language has four typecast operators:
  • static_cast
  • reinterpret_cast
  • const_cast
  • dynamic_cast
Automatic conversions are common in every C++ program.
  1. Standard conversion. For instance: from short to int or from int to float.
  2. User defined conversions (Class conversions.)
  3. Conversion from derived class to base class.
The static_cast can be used for all these types of conversion. Take a look at an example:

 int a = 5;
 int b = 2;
 double out;

 // typecast a to double
 out = static_cast<double>(a)/b;
 
It may take some time to get used to the notation of the typecast statement. (The rumour goes that Bjarne Stroustrup made it difficult on purpose, to discourage the use of typecasting.) Between the angle brackets you place to which type the object should be casted. Between the parentheses you place the object that is casted.

It is not possible to use static_cast on const objects to non-const objects. For this you have to use const_cast. (Further down we take a look at const_cast.)

If an automatic conversion is valid (from enum to int for instance) then you can use static_cast to do the opposite (from int to enum.)

For instance:

 enum my_numbers { a=10, c=100, e=1000 };
 const my_numbers b = static_cast<my_numbers> (50);
 const my_numbers d = static_cast<my_numbers> (500); 
 
 We add some new values (b and d). These are type-cast from int to enum.

Reinterpret_cast

The reinterpret_cast is used for casts that are not safe:
  • Between integers and pointers
  • Between pointers and pointers
  • Between function-pointers and function-pointers
For instance the typecast from an integer to a character pointer:
  
char *ptr_my = reinterpret_cast<char *>(0xb0000);
Example above uses a fixed memory location 


The reinterpret_cast is almost as dangerous as an “old fashion” cast. The only guaranty that you get is that if you cast an object back to the original data-type (before the first cast) then the original value is also restored (of course only if the data-type was big enough to hold the value.)

The only difference with an old fashion cast is that const is respected. This means that a reinterpret_cast can not be used to cast a const object to non-const object.
For instance:
 char *const MY = 0;
 
 // This is not valid because MY is a const!!
 int *ptr_my = reinterpret_cast<int *>( MY); 

Const_cast

The only way to cast away the const properties of an object is to use const_cast.

Take a look at an example:
 void a(Person* b);

 int main()
 {
  const Person *ptr_my = new Person("Joe");
  a( const_cast<Person *>(ptr_my) );
 }
The use of const_cast on an object doesn’t guarantee that the object can be used (after the const is cast away.) Because it is possible that the const-objects are put in read-only memory by the program.

The const_cast can not be used to cast to other data-types, as it is possible with the other cast functions.
Take a look at the next example:

        int *a;
 
        const char *ptr_my = "Hello";
 a  = const_cast<int *>(ptr_my);
 a = reinterpret_cast<const char*>(ptr_my);
 a = reinterpret_cast<int *>(const_cast<char *>(ptr_my) ); 
  1.  The first statement (const_cast) will give an error, because the const_cast can’t convert the type.
  2.  The second statement (reinterpret_cast) will also give an error, because the reinterpret_cast can’t cast the const away. 
  3. The third statement will work (mind the note. It is a dirty trick, better not use it.)
  Lets Learn about Dynamic cast in RTTI


No comments:

Post a Comment