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

static_cast vs reinterpret_cast pointer
static_cast vs dynamic_cast
static_cast vs reinterpret_cast performance
static_cast<char
static_cast vs (int)
static_cast<int to char
reinterpret_cast vs c cast
static_cast c++ int to string

What are the proper uses of:

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast
  • C-style cast (type)value
  • Function-style cast type(value)

How does one decide which to use in which specific cases?


When should static_cast, dynamic_cast, const_cast , const_castcan be used to remove or add const to a variable. static_cast, dynamic_cast, const_cast and reinterpret_cast be used in C++?. You should use it in cases like converting float to int, char to int, etc. dynamic_cast. This cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritence when you cast from base class to derived class. reinterpret_cast. This is the trickiest to use.


Use dynamic_cast for converting pointers/references within an inheritance hierarchy.

Use static_cast for ordinary type conversions.

Use reinterpret_cast for low-level reinterpreting of bit patterns. Use with extreme caution.

Use const_cast for casting away const/volatile. Avoid this unless you are stuck using a const-incorrect API.

[PDF] When should static_cast, dynamic_cast, const_cast and , reinterpret_cast should be used to cast a number to pointer or viceversa: int a = 18;; int* p  You should use it in cases like converting float to int, char to int, etc. dynamic_cast. This cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritance when you cast from base class to derived class. reinterpret_cast. This is the trickiest to use.


(A lot of theoretical and conceptual explanation has been given above)

Below are some of the practical examples when I used static_cast, dynamic_cast, const_cast, reinterpret_cast.

(Also referes this to understand the explaination : http://www.cplusplus.com/doc/tutorial/typecasting/)

static_cast :

OnEventData(void* pData)

{
  ......

  //  pData is a void* pData, 

  //  EventData is a structure e.g. 
  //  typedef struct _EventData {
  //  std::string id;
  //  std:: string remote_id;
  //  } EventData;

  // On Some Situation a void pointer *pData
  // has been static_casted as 
  // EventData* pointer 

  EventData *evtdata = static_cast<EventData*>(pData);
  .....
}

dynamic_cast :

void DebugLog::OnMessage(Message *msg)
{
    static DebugMsgData *debug;
    static XYZMsgData *xyz;

    if(debug = dynamic_cast<DebugMsgData*>(msg->pdata)){
        // debug message
    }
    else if(xyz = dynamic_cast<XYZMsgData*>(msg->pdata)){
        // xyz message
    }
    else/* if( ... )*/{
        // ...
    }
}

const_cast :

// *Passwd declared as a const

const unsigned char *Passwd


// on some situation it require to remove its constness

const_cast<unsigned char*>(Passwd)

reinterpret_cast :

typedef unsigned short uint16;

// Read Bytes returns that 2 bytes got read. 

bool ByteBuffer::ReadUInt16(uint16& val) {
  return ReadBytes(reinterpret_cast<char*>(&val), 2);
}

When should static_cast, dynamic_cast, const_cast and , static_cast It can be used for any normal conversion between types, conversions among static_cast, reinterpret_cast, const_cast, and dynamic_cast to a new C++ should static_cast, dynamic_cast, const_cast and reinterpret_cast be used​? It is necessary from time to time, but should only be used when absolutely necessary. static_cast. static_cast is used for 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 or char to int, etc. dynamic_cast


How to explain the differences among static_cast, reinterpret_cast , When should static_cast, dynamic_cast, const_cast and reinterpret_cast be const_cast can be used to remove or add const to a variable. static_cast should be used when you want to cast from a numeric type to other, from a BaseClass* to a DerivedClass* or from a void* to a specific Type*. Examples: [code]MyStruct* obj = static_cast&lt;MyStruct*&gt;(malloc(sizeof(MyStruct))); int result =


Does this answer your question?

I have never used reinterpret_cast, and wonder whether running into a case that needs it isn't a smell of bad design. In the code base I work on dynamic_cast is used a lot. The difference with static_cast is that a dynamic_cast does runtime checking which may (safer) or may not (more overhead) be what you want (see msdn).

When should static_cast, dynamic_cast, const_cast and , However, these operators can be applied indiscriminately on classes and casting operators: dynamic_cast , reinterpret_cast , static_cast and const_cast . static_cast; dynamic_cast; const_cast; reinterpret_cast; C style cast and function style cast; We’ll go over them one by one, and explain each one. static_cast. static_cast can be used to convert between pointers to related classes (up or down the inheritance hierarchy). It can also perform implicit conversions.


Type conversions - C++ Tutorials, The dynamic_cast<> operator provides a way to check the actual type of a pointer to a (Among new-style casts, only const_cast<> can remove const qualifiers.) The reinterpret_cast operator should not be used to convert between pointers to different A static_cast cannot be used to cast down from a virtual base class. 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.


C H A P T E R 9 - Cast Operations, static_cast; dynamic_cast; const_cast; reinterpret_cast; C-style cast (type) reinterpret_cast is the most dangerous cast, and should be used very sparingly. Const_cast. The expression const_cast<T>(V) can be used to change the const or volatile qualifiers of pointers or references. Where T must be a pointer, reference, or pointer-to-member type.


c++, Const_cast The expression const_cast (V) can be used to change the const or volatile qualifiers of = reinterpret_cast (&s); return 0; } 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