Type casting in C++

const_cast -> casts away the constantness of objects.constant most of the time but that can be changed occasionally.

Example:

1.  string str(“A123456789”);

const char *cstr = str.c_str();

char *nonconst_cstr = const_cast (cstr) ;

2.  Type of this pointer is X* const. Thats why this requires a const_cast,

class MyData
{
public:
/*
the first time, do calculation, cache result in m_lCache, and set
m_bCacheValid to true. In subsequent calls, if m_bCacheValid is true
then return m_lCache instead of recalculating
*/

long ExpensiveCalculation() const
{
if (false == m_bCacheValid)
{
MyData* fakeThis = const_cast<MyData*>this;
fakeThis->m_bCacheValid = true;
fakeThis->m_lCache = ::SomeFormula(m_internalData);
}
return m_lCache;
};

private:

data m_internalData;
long m_lCache;
bool m_bCacheValid;

};

 
reinterpret_cast -> converting 1 pointer to another , no type checking is performed

Not portable.

A typical example is an int-to-pointer conversion to get a machine address into a program:

Example:

Register* a = reinterpret_cast(0xfa);
static_cast : static_cast performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary.It also only allows casting between related types.

such as non-const object to const, int to double. It can be also be used to perform the reverse of many conversions such as void* pointers to typed pointers.

Example:

// #1: valid upcast Base *pBase = static_cast(&derived);

// #2: valid downcast Derived *pDerived = static_cast (&base);

Static_cast vs Dynamic_cast:
B* b = new B();
D* d1 = static_cast<D*>b; // Invalid! But Allowed
D* d2 = dynamic_cast<D*>b; // Valid, but d2 is now a null pointer
Now d1 will point to a data segment of type D*, but the actual data is B*, and will lead to memory issues and corruption. d2 on the other hand will be a null pointer and can be checked for and handled correctly.
But since dynamic_cast performs runtime type checking it is slower.

An “up-cast” is always valid with both static_cast and dynamic_cast, and also without any cast, as an “up-cast” is an implicit conversion.

Important use case of static cast :

Its used when typecasting of many instances are required so in that case calling dynamic cast for each issue will be time taking So once is sure for one value it can be allowed for entire collection.

dynamic_cast: gives error if u try to downcast or the class is not polymorphic(A class that declares or inherits a virtual function)

Regular cast safely cast to a private base-class, while the “equivalent” static_cast sequence would give you a compile time error for that.

Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion. //like double to int.
When dynamic_cast cannot cast a pointer because it is not a complete object of the required class (as in the second conversion in the previous example), it returns a null pointer to indicate the failure. If  dynamic_cast  is used to convert to a reference type and the conversion is not possible, an exception of type bad_cast is thrown.

C++ C C# Health concepts Leetcode Programming Data Streucture