Casting is a conversion process of changing some data into a different type of data. We can convert between built-in types or our own datatypes. Some of the conversions are done automatically by the compiler, and the programmer does not have to intervene. Such conversions are called implicit conversions. Other conversions, which have to be directly specified by the programmer, are called explicit conversion. Sometimes we may get warnings about loss of data. We should pay heed to these warnings and think about how this might adversely affect our code. Casting is commonly used when the interface expects a particular type, but we want to feed it data of a different type. With C, we can cast anything to everything. However, C++ provides us with finer controls.
In this recipe, we will see how we can easily cast or convert between various datatypes. Usually, a programmer uses C-style casting even in C++, but this is not recommended. C++ provides us with its own style of casting for different situations which we should use:
Open Visual Studio.
Create a new C++ project.
Select Win32 Console Application.
Add a source file called
main.cpp
or anything that you want to name the source file.Add the following lines of code:
#include <iostream> #include <conio.h> using namespace std; int main() { int iNumber = 5; int iOurNumber; float fNumber; //No casting. C++ implicitly converts the result into an int and saves //into a float fNumber = iNumber/2; cout << "Number is " << fNumber<<endl; //C-style casting. Not recommended as this is not type safe fNumber = (float)iNumber / 2; cout << "Number is " << fNumber<<endl; //C++ style casting. This has valid constructors to make the casting a safe one iOurNumber = static_cast<int>(fNumber); cout << "Number is " << iOurNumber << endl; _getch(); return 0; }
There are four types of casting operators in C++, depending on what we are casting: static_cast
, const_cast
, reinterpret_cast
, and dynamic_cast
. Now, we are going to look at static_cast
. We will look at the remaining three casting technique after we discuss dynamic memory and classes. Converting from a smaller datatype to a larger type is called promotion and is guaranteed to have no data loss. However, conversion from a larger datatype to a smaller one is called demotion and may lead to data loss. Compilers will generally give you a warning when this happens, and you should pay heed to this.
Let us look at the previous example. We have initialized an integer with the value 5
. Next, we have initialized a floating point variable and stored the result of 5
divided by 2
, which is 2.5
. However, when we display the variable fNumber
, we see that the displayed value is 2
. The reason is the C++ compiler implicitly casts the result of 5/2
and stores it as an integer. So it is evaluating something similar to int (5/2
) which is int (2.5
), evaluating to 2
. So to achieve our desired result, we have two options. The first method is a C-style explicit cast, which is not recommended at all because it does not have a type safe check. The format for the C-style cast is (resultant_data_type
) (expression
), which in this case is something like float (5/2
). We are explicitly telling the compiler to store the result of the expression as a floating point number. The second method, and a more C++ style way of doing the cast, is by using the static_cast
operation. This has suitable constructors to dictate that the conversion is type safe. The format for a static_cast
operation is static_cast<resultant_data_type> (expression)
. The compiler checks if the casting conversion is safe and then executes the type casting operation.