Updated June 29, 2023
Introduction to C++ static_cast
The static_cast operator in C++ serves as the operator that converts a variable from one data type to another, with a particular focus on transforming it into a float data type. The compiler performs this conversion exclusively with the static_cast, paying constant attention to const types, similar to const_cast and reinterpret_cast, which perform casting from one type to another. This casting technique utilizes both implicit and explicit conversions. The constructor applies compile-time operations exclusively, ensuring that it does not throw any exceptions.
Syntax
The static_cast operator is used to convert variables to different data types, including float types, in C++. It can also be used for conversions involving pointers. Each C++ method, operator, and variable can have proper syntax and formats for creating the applications.
#include <iostream>
using namespace std;
datatype main()
{
data type v;
data type variable name=static_cast<data type>(v);
}
The above code is the basic syntax for using the static_cast operator in the programming logic, and these operator conversions will not throw any exceptions because it is used in the compile time.
How does the static_cast Method work in C++?
The casting conversion is the general thing of the programming language because it converts from one type into another data type. When we use implicit conversions sequence from any type of expression into the new types, if the constructor loads any arguments and calls the static_cast operator, it creates at least one type of object for calling the methods in some other areas. The object reference is of any type. It also accepts some new types from any type of expression, and also it finds at least any viable functions so that the static_cast<new_type>(expression) is used, and it accepts the args constructor it returns some other virtual imaginary variables. The temp variable has passed the arguments involving the implicit type conversions to call the constructors. It may be any of the types.
When called the new type conversions, it defines the user-defined conversion from the operator. The object sourcing type is different, and the destination or target type is the Boolean type or bool type. It represents the enum with the target reference type, allowing only the true and false conditions.
Examples
Lets us discuss examples of C++ static_cast.
Example #1
Code:
#include <iostream>
#include <string>
using namespace std;
class demo {
int a;
public:
demo(int a_i = 1)
: a{a_i }
{
cout << "The conversion is called through this method" << endl;
}
operator string()
{
cout << "The casting conversion operation is:" << endl;
return to_string(a);
}
};
int main()
{
demo b(4);
string s = b;
b = 32;
string s1 = static_cast<string>(b);
b = static_cast<demo>(34);
return 0;
}
Output:
Example #2
Code:
#include <iostream>
using namespace std;
class first {
public:
int i, j;
first(int a1 = 18, int b1= 23) {
i = a1; j = b1;
}
void demo() {
cout<<"The first class object is: i = "<< i <<" , j = " <<j<< endl;
}
~first() {
}
};
class second : public first {
public:
int p;
second(int p1 = 34) { p = p1; }
void demo() {
cout << "The scond class object is:i = " << i << " , j = " << j << " , p = " << p << endl;
}
~second() {
}
};
int main() {
first* f = new first(18, 24);
second* s = new second(8);
f->demo();
s->demo();
second* s1 = static_cast<second*>(f);
cout << "The first class casting is doen to the second class, ";
s1->demo();
first* f1 = static_cast<first*>(s);
cout << "The second class casting is done to teh first class, ";
f1->demo();
delete f;
delete s;
return 0;
}
Output:
Example #3
Code:
#include <iostream>
template <class A>
bool demo(A *i) { return false;}
template <>
bool demo(void *i) { return true; }
using namespace std;
int main() {
char* a = (char*)"Welcome To My Domain";
void* p = static_cast<void*>(a);
cout << "Have a Nice day user " << (demo(p) ? "The pointer is used click yes " : "The pointer is not used click no") << endl;
cout << static_cast<char*>(p) << endl;
return 0;
}
Output:
In the above examples, we used the static_cast operator in different ways. We also used a pointer for referring to the parent class and child class relationship.
Conclusion
C++ uses many different operators, and each of them has different usages here; the static_cast is mainly used for the operator conversions, and it’s not affected by the other areas of the programming logic; hence it executed in the compile-time itself, so it does not throw any runtime exceptions it’s one of the great advantages of this operator.
Recommended Articles
This is a guide to C++ static_cast. Here we also discuss the introduction and How the static_cast Method Work in C++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –