Four casting types are supported by C++:
- Static Cast
- Dynamic Cast
- Const Cast
- Reinterpret Cast
In this guide, we will delve into a detailed exploration of the static_cast operator.
Static Cast:
The most basic casting method available is the static cast. This type of casting occurs during compilation and has the capability to invoke explicit conversion functions along with executing implicit type conversions like transforming an integer to a floating-point number or a pointer to a void pointer.
Syntax of static_cast
The syntax of static_cast are as follows:
static_cast<dest_type> (source);
The return value of staticcast will be assigned to desttype.
A static_cast example
The C++ code snippet demonstrating the usage of static_cast is presented here:
#include <iostream>
using namespace std;
int main()
{
float f = 5.6;
int a = f;
cout<< "The Value of a: " << a;
int b = static_cast<int>(f);
cout<< "\nThe Value of b: " << b;
}
Output:
The Value of a: 5
The Value of b: 5
Different Scenarios and the Behaviour of static_cast:
- When dealing with references to basic data types, static_cast is employed:
Let's now modify the code mentioned above a bit.
// A static_cast char* to int* demonstration program in C++
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 6;
char c = 'c';
// It may succeed at compile time but fail at run time.
int* q = (int*)&c;
int* p = static_cast<int*>(&c);
return 0;
}
Output:
It will give us an error like;
error: invalid 'static_cast' from type 'char*' to type 'int*'
Explanation:
It signifies that using static_cast will prohibit the conversion of an object into another type, even if it goes against the rules.
- Utilizing a User-Defined Conversion Operator for Object Conversion
If a class has a conversion operator defined, the static_cast function can utilize it. Consider another example of an object being converted to or from a class.
// Cast class object to string object using a C++ program.
#include <iostream>
#include <string>
using namespace std;
class integer {
int x;
public:
// constructor
integer(int x_in = 0)
: x{ x_in }
{
cout<< "Constructor is Called" <<endl;
}
// to a string type with user-defined conversion operator
operator string()
{
cout<< "now Conversion Operator is Called" <<endl;
return to_string(x);
}
};
int main()
{
integer obj(3);
string str = obj;
obj = 20;
// typecasting with static_cast
string str2 = static_cast<string>(obj);
obj = static_cast<integer>(30);
return 0;
}
Output:
Constructor is Called
now Conversion Operator is Called
Constructor is Called
now Conversion Operator is Called
Constructor is Called
Explanation:
Let's try to figure out the output from above, line by line:
- The constructor , which in this case is actually a conversion constructor (C++14 regulations have changed slightly), is called when the object (obj) is created.
- Since we defined the Conversion operator , the compiler won't throw an error when you generate str from obj .
- You are actually invoking the conversion constructor when you make obj = 20 .
- Making str2 from static_cast is very similar to making string str = obj ; with strict type checking.
- Staticcast is used to change 30 into an integer when you write obj = staticcast Int> (30) .
- C++'s static_cast for inheritance
In situations involving inheritance, staticcast can facilitate both upcasting and downcasting. An illustration showcasing the application of staticcast for upcasting is demonstrated below.
// An inheritance-related static_cast example in C++
#include <iostream>
using namespace std;
class Base
{};
class Derived : public Base
{};
// Driver code
int main()
{
Derived d1;
// acceptable implicit casting
Base* b1 = (Base*)(&d1);
// using static_cast to upcast
Base* b2 = static_cast<Base*>(&d1);
return 0;
}
Explanation:
The above code will compile perfectly.
- We explicitly cast the address of d1 into Base and saved it in b1 .
- We got the address from d1 , converted it to Base using static_cast , and then put it in b2.
- In the above example, the base class was inherited as public .