static_cast can perform an explicit type of casting-there are two types of casting implicit and explicit.This post explains how to use static_cast function to perform all the possible casting from one type to another type.The casting between different data types that will be discussed in this post is shown below.
Casting from int to float type
While performing mathematical operations converting int to float type may be necessary.In such case we don’t need to sweat over it,you can simply use the static_cast to convert the type.
int num=90 ;
float result=static_cast<float>(num)/static_cast<float>(80) ;
Note here the denominator value 80 is a double type,it is converted to float type using the static_cast.Why this conversion is necessary is explained in another post the link is provided below.
Casting from float to int
Casting from floating point type to integer type can create a dangerous situation for us.Suppose if we cast a floating point number with a fractional value to int type,the fractional value is lost.In mathematical operation this will render us a less precision value which in fact can give rise to various errors.So it is always advised to minimize casting in their programs.Keeping this topic-the danger of casting- aside for now the code below performs float to int type casting using static_cast function.
int product=78 * static_cast<int>(val) ;
cout<< product ;
Casting from float to char
Since char has the same internal format as the integer type,casting character type to float type is acceptable in C++.So the normal rules apply here.
char c=static_cast<char>(45) ;
cout<< c ;
The output is,
Since the value 45 maps to ‘-‘ in ASCII chart hence the output.
Casting char to float
Casting from char to float type gives the integer value of the character as given in ASCII chart.The code below performs casting from char to float type.
float f=static_cast<float>(c) ;
cout<< f ;
Casting int to char and vice versa
Integer and character type has the same internal format so whether you cast it using static_cast or not ,either way it works fine.
char c=val , cc= static_cast<char>(val) ;
Casting from char to string
Casting from char to string type using static_cast function is not possible.Though,char type holds a letter and string type can hold a collections of letters it seems these two are treated as a completely different types;not exaggerating even to say int and char type are treated more equally.
string s=static_cast<string>(c) ;///error
Although static_cast does not support casting from char to string type we cannot give up yet,can we? So let’s design a simple function that can convert char to string type.Since every char variable consists of only one character what our function will do is assign that character to C type string-char array- and assign the C type string to C++ type string i.e string type.The simple function is shown below.
string char_to_string(const char c )
char cstr=”” ; //C type string
return cstr ;
int main( )
char c=’G’ ;
string s=char_to_string(c) ;
cout<< s ;
The output is,
Since char variable hold only one letter converting the char type to string type should also consist of only letter.
Casting from string to char
Converting string to char type with static_cast is not possible.Even if it is possible it wouldn’t make any sense.So in this case even if we try to develop a new function to convert string to char type,which letter would we choose from the multiple letters of the string to assign to the char variable;undoubtedly a hard decision.
Stop wasting time,earn money($$$) from your website-Join Now!
Casting from void pointer to int,char, float and string pointer
I am sure you know void means “no type”.Since void type is not bound to any type specifically void pointer can point to any of the built-in type.Now void pointer pointing to the built-in type means casting is involve, fortunately casting void pointer to any of the type falls in the hand of static_cast function.Casting void pointer to int or float or char or string pointer follows the normal rule.A code example is shown below.
char c=’B’ , *cpt=&c ;
float f=89.899 , *fpt=&f ;
string str=”Shoe” , *spt=&str ;
void *vdi =static_cast<void* >(ipt) ,
*vdc=static_cast<void* >(cpt) ,
*vdf=static_cast<void* >(fpt) ,
*vds=static_cast<void* >(spt) ;
void v=static_cast<void>(ival) ;///error
Casting a built-in data type variable to void type is not allowed.
Casting the void pointer back to it’s original type is discuss in the next section.
Casting from int,char,string,float pointer to void pointer
If any of the data type pointer is casted to void pointer type ,we should cast it back to the original type to assign it to the original type pointer.The static_cast can also handle this operation.Consider the code below.
int *ip=static_cast<int*>(vdi) ;
string *sp=static_cast<string*>(vds) ;
Becareful when casting void pointer back to the original type,if you get the type wrong,you will most probably get some undefined value when trying to access the value using the pointer which is assigned the void pointer.
float *fp=static_cast<float*>(vdi) ;
cout<< *fp ; //undefined value
Using static_cast to cast string to integer or floating point type is not possible.But there are some other ways to achieved this result.Another post is dedicated for the discussion of this type of casting.The methods discussed uses the C++ ways,C ways and using the Boost library.The link is provided below.