Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C++ static_cast

Introduction to C++ static_cast

The C++ static_cast is defined as the Operator which has to convert the variable from one data type into another data type mainly it transform into float data type the compiler only done this conversion in the static_cast because it constantly focus on the const types like const_cast, reinterpret_cast it also casting from one type into another type same like casting technique it uses both the implicit and explicit conversions the constructor which is used for applying only the compile-time so it can’t throw any exceptions and also if sometimes the casting is not done in any lines it will not compiled.

Syntax:

Static_cast is like an operator is used to casting the variables into the float types. The pointer also included in these conversions and also it applies both implicit and explicit conversion functions. For each c++ methods, operators, and other variables, they can have proper syntax and formats for creating the applications.

#include
using namespace std;
datatype main()
{
data type v;
data type variable name=static_cast(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 static_cast Method works in C++?

The casting conversion is the general thing of the programming language because it converts from one type into another data type. Likewise, static_cast is the operator and is used for done the casting operations in the compile timeWe already said that the casting is done for both implicit and explicit conversions. If when we use implicit conversions sequence from any type of expressions into the new types if the constructor loads any arguments it calls the static_cast operator it creates atleast 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 expressions and also it finds at least any viable functions so that the static_cast(expression) is used and it accepts the args constructor it returns some other virtual imaginary variables. Normally the variable named it as Temp the temp variable has passed the arguments and it involves 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 main referenced and non-referenced objects it’s used and the output result of the object is in static_cast operator and the value with expressions also the pointer is taking the general role and it uses all the type conversions. Like the object referenced is pointed to any pointer variable it also calculates the expressions and these expressions and values will be the non-virtual object reference and the pointer values are pointed into it. The static _cast performs the downcasting because its in the ill-formed and the variables are also the ambiguous one inaccessible to the virtual objects. Such types of static_cast is made for only the run time validation and to ensure the objects are presented in the runtime because it’s a safety purpose and also it checks the pre-validated and satisfy with the other conditions to guaranteed with the other implementing mechanism in the C++ concepts like polymorphism, inheritance, etc. Safely the static_cast is done with the downcast using the dynamic_cast operations when compare to the static_cast.

We can use enumeration type in the c++ and its type is converted to the integer or some other types. The object sourcing type is different and the destination or target type is the Boolean type or bool type. It allows only the true and false conditions and enum is represented with the target reference type.

Examples

Lets us discuss examples of C++ static_cast.

Example #1

Code:

#include
#include
using namespace std;
class demo {
int a;
public:
demo(int a_i = 1)
: a{a_i }
{
cout }
operator string()
{
cout return to_string(a);
}
};
int main()
{
demo b(4);
string s = b;
b = 32;
string s1 = static_cast(b);
b = static_cast(34);
return 0;
}

Output:

Example #2

Code:

#include
using namespace std;
class first {
public:
int i, j;
first(int a1 = 18, int b1= 23) {
i = a1; j = b1;
}
void demo() {
cout }
~first() {
}
};
class second : public first {
public:
int p;
second(int p1 = 34) { p = p1; }
void demo() {
cout }
~second() {
}
};
int main() {
first* f = new first(18, 24);
second* s = new second(8);
f->demo();
s->demo();
second* s1 = static_cast(f);
cout s1->demo();
first* f1 = static_cast(s);
cout f1->demo();
delete f;
delete s;
return 0;
}

Output:

Example #3

Code:

#include
template
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(a);
cout cout (p) return 0;
}

Output:

The above examples we used static_cast operator in different ways we also used pointer for referred the parent class and child class relationship.

Conclusion

In C++ used many different operators and each of them have different usages and here the static_cast is mainly used for the operator conversions and its not affected with the other areas of the programming logic hence its 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 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 –

  1. C++ fstream
  2. C++ sort()
  3. C++ Mutable
  4. C++ Formatter

The post C++ static_cast appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

C++ static_cast

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×