Javatpoint Logo
Javatpoint Logo

Static cast in C++

C++ programming is a strong and flexible language that offers a few typecasting options. The static cast is one of these techniques which enables programmers to change one type to another explicitly. In this blog article, we will examine the syntax, applications, and advantages of C++'s static cast. We will also offer code samples and their associated results to aid your understanding. By the conclusion, you'll have a firm grasp on the static cast and feel confident using it in your C++ programs.

Type casting in C++ is the transformation of one data type into another. Use the static cast operator to do explicit type conversions simply and securely. Because the type conversions are handled at compile time rather than run time, it is faster and less prone to errors, hence the name "static".

The syntax for a static cast is as follows:

static_cast<new_type>(expression);

In this case, the expression denotes the value or variable to be cast, while new_type stands for the desired type. If the conversion is a legitimate and clearly specified operation, the static cast operator makes an attempt to carry it out.

Basic Static Cast

Let's start with an easy example to show how static cast functions. Think about a situation when you want to change an integer into a floating-point number. Here is the key:

Output:

Result: 42.0

Explanation:

In this illustration, we explicitly transform the integer num to a floating-point number using a static cast. The conversion is carried out via the static cast operator (float), and the outcome is saved in the result variable. As 42 is shown as 42.0 in the output, the conversion was successful.

Class Conversion with Static Cast

If there is a legitimate relationship between the classes, such as inheritance or explicit conversion operators, static cast can also be used to convert pointers or references between classes. Let's use the following case to demonstrate:

Output:

Derived class
Derived class

Explanation:

In this illustration, we have a base class called Base and a derived class called Derived that derives from Base. We generate a basePtr pointer of type Base* and give it the address of a Derived object that is dynamically allocated. We utilize a static cast to change basePtr into a pointer of type Derived* and assign it to derivedPtr. The output demonstrates that the overridden method in the derived class is successfully invoked after using both pointers to call the display() function.

Conclusion:

Finally, static cast is a key component of C++ that enables programmers to execute explicit type conversions. It provides a secure and quick mechanism to convert between various kinds, whether they are classes with inheritance relationships or primitive types. Understanding static cast's syntax and application gives programmers more control over type conversions, assuring type safety and effectiveness.

In this blog post, we looked at the static cast syntax and included thorough code examples with appropriate results. We showed how static cast can be used in a variety of situations, from straightforward conversions between primitive types to more complicated conversions requiring class hierarchies.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA