c++ - When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? -
what proper uses of:
static_castdynamic_castconst_castreinterpret_cast- c-style cast
(type)value - function-style cast
type(value)
how 1 decide use in specific cases?
static_cast first cast should attempt use. things implicit conversions between types (such int float, or pointer void*), , can call explicit conversion functions (or implicit ones). in many cases, explicitly stating static_cast isn't necessary, it's important note t(something) syntax equivalent (t)something , should avoided (more on later). t(something, something_else) safe, however, , guaranteed call constructor.
static_cast can cast through inheritance hierarchies. unnecessary when casting upwards (towards base class), when casting downwards can used long doesn't cast through virtual inheritance. not checking, however, , undefined behavior static_cast down hierarchy type isn't type of object.
const_cast can used remove or add const variable; no other c++ cast capable of removing (not reinterpret_cast). important note modifying formerly const value undefined if original variable const; if use take const off reference wasn't declared const, safe. can useful when overloading member functions based on const, instance. can used add const object, such call member function overload.
const_cast works on volatile, though that's less common.
dynamic_cast exclusively used handling polymorphism. can cast pointer or reference polymorphic type other class type (a polymorphic type has @ least 1 virtual function, declared or inherited). can use more casting downwards -- can cast sideways or chain. dynamic_cast seek out desired object , return if possible. if can't, return nullptr in case of pointer, or throw std::bad_cast in case of reference.
dynamic_cast has limitations, though. doesn't work if there multiple objects of same type in inheritance hierarchy (the so-called 'dreaded diamond') , aren't using virtual inheritance. can go through public inheritance - fail travel through protected or private inheritance. issue, however, such forms of inheritance rare.
reinterpret_cast dangerous cast, , should used sparingly. turns 1 type directly - such casting value 1 pointer another, or storing pointer in int, or sorts of other nasty things. largely, guarantee reinterpret_cast if cast result original type, exact same value (but not if intermediate type smaller original type). there number of conversions reinterpret_cast cannot do, too. it's used particularly weird conversions , bit manipulations, turning raw data stream actual data, or storing data in low bits of aligned pointer.
c-style cast , function-style cast casts using (type)object or type(object), respectively. c-style cast defined first of following succeeds:
const_caststatic_cast(though ignoring access restrictions)static_cast(see above),const_castreinterpret_castreinterpret_cast,const_cast
it can therefore used replacement other casts in instances, can extremely dangerous because of ability devolve reinterpret_cast, , latter should preferred when explicit casting needed, unless sure static_cast succeed or reinterpret_cast fail. then, consider longer, more explicit option.
c-style casts ignore access control when performing static_cast, means have ability perform operation no other cast can. kludge, though, , in mind reason avoid c-style casts.
Comments
Post a Comment