static_cast
will not throw an exception. But if it does not succeed, it will produce a undefined result. What is the most proper way to check whether the cast succeeded?
Will this help?
NewType new_typ_obj = static_cast<NewType>(obj);
if (new_typ_obj)
new_typ_obj.do();
C++ Server Side Programming Programming. The static_cast is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coercion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can cast related type classes.
This is also the cast responsible for implicit type coercion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can cast related type classes. If the types are not same it will generate some error.
You can't static_cast between references (or pointers) to "unrelated types." While you could static_cast from a CMyObject* to a CObject*, that isn't what you're doing here.
C casts are casts using (type)object or type (object). A C-style cast is defined as the first of the following which succeeds: const_cast static_cast (though ignoring access restrictions) static_cast (see above), then const_cast reinterpret_cast reinterpret_cast, then const_cast
static_cast does not give you information about success. If you need to do dynamic type casting use dynamic_cast or a library like boost any.
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