Home | API | MFC | C++ | C | Previous | Next

Programming With C++

Type Conversions

C++ supports two types of conversions

Implicit Type Conversion

Is an automatic type conversion by the compiler. Implicit type conversion happens automatically when a value is copied to a compatible data type.  It is possible for implicit conversions to lose information for instance when a signed is implicitly converted to unsigned and when a long is implicitly converted to float.

#include<stdio.h>
int main()
{
short a=50;
int;
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
}

Explicit Type Conversion  

Also known as “type-casting” is a user-defined conversion with the programmer explicitly defining the casting.  The process is usually associated with information loss. 

Explicit conversion can be performed by using an assignment operator or by using a cast operator

Using the  Assignment Operator

The Assignment  operator forces one data type to be converted into another and consists of the type name before an expression

#include <iostream>
using namespace std;
int main()
{
double x= 3.14159265359;
int y = (int)x; //using cast operator (int)
cout<<"x = "<<x <<"\n";
cout<<"y = "<<y;
return 0;
}

Using Cast Operator

C++ supports four types of casting using four specific casting operators: static_cast, dynamic_cast, reinterpret_cast,  and const_cast.

Static Cast – Used for implicit conversions between types such as int to float.  It also performs conversions between pointers of classes related to each other by ‘upcasting’ from a derived to base class and ‘downcasting’ from base to derived class.

#include <iostream>
using namespace std;
int main()
{
double p = 3.14159265359;
cout<<"Before casting: pi = "<<p<<endl;
int total = static_cast<int>(p);
cout <<"After static_cast:total = "<<total;
}

Dynamic Cast – Dynamic cast is a runtime cast performed only on class pointers and references. The most common use for dynamic casting is for converting(downcasting) a base-class pointer into a derived class.

#include <iostream>
using namespace std;
class base {public: virtual void bmethod(){}};
class derived:public base{};
int main()
{
base* b = new derived;
derived* d = dynamic_cast<derived*>(b);
if(d != NULL)
cout<<"Dynamic cast successful";
else
cout<<"Dynamic cast not successful";
}

Reinterpret Cast – reintepret_cast works on pointers and converts a pointer of any type to any other type of pointer without checking if the pointer or the data pointed to by the pointer is the same or not. 

#include <iostream>
using namespace std;
int main()
{
int* ptr = new int(100);
char* ch = reinterpret_cast<char*>(ptr);
cout << *ptr << endl;
cout << *ch << endl;
return 0;
}

Const Cast – const_cast is used to remove the constant attribute from references and pointers.  It is not permitted to const_cast variables that are actually const.  The pointer or reference and the source which is cast should be of the same type.

#include <iostream>
using namespace std;
int main(void)
{
const double x = 3.14159265359;
const double* y = &x;
cout << "old value is " << * y << "\n";
double* z=const_cast<double *>(y);
*z=3.14;
cout<<"new value is "<<*y;
}

Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables and Constants | Arrays | C-strings | Expressions and Operators | Controlling Program Flow | C++ Functions | Pointers and References | Memory Map and Free Store | Smart Pointers | Classes | Structures | Inheritance | Polymorphism | Templates | The Standard Template Library | The STL String Class | Namespace | Type Conversions | Input and Output Streams | The C++ Preprocessor | Exception Handling

Last Updated: 15 September 2022