by
edited by

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

1 Answer

0 votes
by
 
Best answer

static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn't necessary, but it's important to note that the T(something) syntax is equivalent to (T)something and should be avoided (more on that later). A T(something, something_else) is safe, however, and guaranteed to call the constructor.

static_cast can also cast through inheritance hierarchies. It is unnecessary when casting upwards (towards a base class), but when casting downwards it can be used as long as it doesn't cast through virtual inheritance. It does not do checking, however, and it is undefined behavior to static_cast down a hierarchy to a type that isn't actually the type of the object.


const_cast can be used to remove or add const to a variable; no other C++ cast is capable of removing it (not even reinterpret_cast). It is important to note that modifying a formerly const value is only undefined if the original variable is const; if you use it to take the const off a reference to something that wasn't declared with const, it is safe. This can be useful when overloading member functions based on const, for instance. It can also be used to add const to an object, such as to call a member function overload.

const_cast also works similarly on volatile, though that's less common.


dynamic_cast is exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You can use it for more than just casting downwards – you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can't, it will return nullptr in the case of a pointer, or throw std::bad_cast in the case of a reference.

dynamic_cast has some limitations, though. It doesn't work if there are multiple objects of the same type in the inheritance hierarchy (the so-called 'dreaded diamond') and you aren't using virtual inheritance. It also can only go through public inheritance - it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.


reinterpret_cast is the most dangerous cast, and should be used very sparingly. It turns one type directly into another — such as casting the value from one pointer to another, or storing a pointer in an int, or all sorts of other nasty things. Largely, the only guarantee you get with reinterpret_cast is that normally if you cast the result back to the original type, you will get the exact same value (but not if the intermediate type is smaller than the original type). There are a number of conversions that reinterpret_cast cannot do, too. It's used primarily for particularly weird conversions and bit manipulations, like turning a raw data stream into actual data, or storing data in the low bits of a pointer to aligned data.


C-style cast and function-style cast are casts using (type)object or type(object), respectively, and are functionally equivalent. They are 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

It can therefore be used as a replacement for other casts in some instances, but can be extremely dangerous because of the ability to devolve into a reinterpret_cast, and the latter should be preferred when explicit casting is needed, unless you are sure static_cast will succeed or reinterpret_cast will fail. Even then, consider the longer, more explicit option.

C-style casts also ignore access control when performing a static_cast, which means that they have the ability to perform an operation that no other cast can. This is mostly a kludge, though, and in my mind is just another reason to avoid C-style casts.

Related questions

0 votes
    I can't find much information on const_cast. The only info I could find (on Stack Overflow) is: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am using exit(), quit(), os._exit(), sys.exit() to stop script execution, and I am confused which ... use to stop script execution? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to implement in Java a class for handling graph data structures. I have a Node class and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    What data structure should be used when number of elements is fixed? (a) Array (b) Array list (c) Vector ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What data structure should be used when number of elements is fixed? (a) Array (b) Array list (c ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    I am training on 970 samples and validating on 243 samples. How big should batch size and number of epochs be ... on data input size? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    We have a service method GetDataParallel( ) which maybe called by many clients currently, and we use the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    Is there any reason to keep time series data in its own database, separate from other tables or is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    Give scientific reasons: Antibiotics should be taken only when prescribed by a doctor. Select the correct ... proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    When the polarisation of the receiving antenna is unknown, to ensure that it receives atleast half ... by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 6, 2021 in Education by JackTerrance
0 votes
    _________ refers to an undesirable state when a system attempts to perform two or more operations, which should, ... condition 2. Optimistic locking 3. Locking 4. Transaction...
asked Apr 12, 2021 in Technology by JackTerrance
0 votes
    What type of testing should be taken into consideration according to you when the regression tests are repeated multiple times?...
asked Feb 7, 2021 in Technology by JackTerrance
0 votes
    What should be taken into account when estimating story point size...
asked Nov 30, 2020 in Technology by Editorial Staff
0 votes
    If you decided to open a media house and you planned to hire workers for financial, graphic design and ... 50% Identification 50% Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
...