What is a cast operator? What types of casting can be used in C++? What is a static_cast in C++? How can I use static_cast in C++ Software?
In C++, a cast operator is an Unary Operator which forces one data type to be converted into another data type.
In general, C++ supports four types of casting:
- Static Cast
- Dynamic Cast
- Const Cast
- Reinterpret Cast
What is a Static Cast?
In C++, static cast converts between types using a combination of implicit and user-defined conversions. In another term a static_cast returns a value of type new_type
.
A static_cast
is usually safe. There is a valid conversion in the language, or an appropriate constructor that makes it possible. The only time it’s a bit risky is when you cast down to an inherited class; you must make sure that the object is actually the descendant that you claim it is, by means external to the language like a flag in the object.
This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.
As with all cast expressions, static_cast can be used on,
- an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
- an xvalue if new_type is an rvalue reference to object type;
- a prvalue otherwise.
We can say that two objects a and b are pointer-interconvertible if
- they are the same object, or
- one is a union object and the other is a non-static data member of that object, or
- one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no non-static data members, any base class subobject of that object, or
- there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.
Is there an example of how to use static_cast?
Here is a simple example of cast which can be used. This is also called a compile time cast. It converts int to float, or pointer to void* in an implicit way, and it can be called with explicit conversion functions (or implicit ones). Here is a simple example of how to cast a float number as an int number.
1 2 3 4 |
float paramf; int parami = static_cast< int >( paramf ); |
Here is the full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> int main() { float paramf; int parami; paramf = 7.01; parami = static_cast< int >( paramf ); std::cout << paramf << " => " << parami << std::endl; paramf = 7.50; parami = static_cast< int >( paramf ); std::cout << paramf << " => " << parami << std::endl; paramf = 7.99; parami = static_cast< int >( paramf ); std::cout << paramf << " => " << parami << std::endl; getchar(); return 0; } |
and the output will be
1 2 3 4 5 |
7.01 => 7 7.5 => 7 7.99 => 7 |
This is a good example how we can use static_cast between classes,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> using namespace std; class TMyBase { }; class DerivedClass : public TMyBase { }; class CMyOtherClass { }; int main() { TMyBase *base; DerivedClass *object; object = static_cast<DerivedClass*>(base); } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition