static_cast<>() gives you a compile time checking ability, C-Style cast doesn't. static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt. Intentions are conveyed much better using C++ casts.
A cast is a special operator that forces one data type to be converted into another. As an operator, a cast is unary and has the same precedence as any other unary operator. const_cast<type> (expr) − The const_cast operator is used to explicitly override const and/or volatile in a cast.
In order to control these types of conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.
Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?
I have done a lot of googling to find about:
The following is what I found:
Now let me brief state why and when for each of the C++ casting operators
static_cast:
Why use it over C style casting?
static_cast
is used to perform conversion between related types.
Examples :
Class A {};
Class B {};
A* a = new A();
B* b = static_cast<B*>(a); // Compiler error
B* b1 = (A*)a; // Works fine
float f;
int addr = (int)(&f); // Works fine
int addr = static_cast<int>(&f); // Compiler error
But I wanted to know a real use case of when to use above code?
reinterpret_cast :
reinterpret_cast
casts pointers to unrelated types.
Examples:
Class A {};
Class B {};
A* a = new A();
B* b = reinterpret_cast<B*>(a); // Works fine
B* b1 = (A*)a; // Works fine
float f;
int addr = (int)(&f); // Works fine
int addr = reinterpret_cast<int>(&f); // Works fine
int ai = 10;
float af = 13.33;
// Would depend on how floating point is stored in machine
// int& since reinterpret_cast expects either the type or operand to be pointer or reference
int ki = reinterpret_cast<int&>(af); // ki would not be 13
int kitemp = (int)af; // kitemp would be 13
// The same reinterpret_cast behaviour can be achieved using this,
int* in = (int*)(af);
cout << (*in);
My question is how else is reinterpret_cast
different from C style casting? I'm unable to find why to use it over traditional casting operators and also when to use it?
Another important example which makes these operators worse is:
const unsigned int * p;
(int*)p; // Would remove unsigned and const at one shot
// Using C++ casting operators
// Const_cast expects a pointer or a reference
reinterpret_cast<int*>(const_cast<unsigned int* >(p));
Writing the above code to remove const
and unsigned
is much more complex in C++ casting? Then why do people use reinterpret_cast
, const_cast
or static_cast
over tradional C casting operators?
I do understand about dynamic_cast
used in case of polymorphic classes; again this operator too has an extra cost of RTTI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With