Explain the use of const_cast and reinterpret_cast operators in C++

  • 2020-05-07 20:10:14
  • OfStack

const_cast operator
removes the const, volatile, and arbitration 8en features from the class.
grammar


const_cast < 
type-id
 > ( 
expression
 )

note
A pointer to any object type or to a data member can be explicitly cast to the exact same type (with the exception of the const, volatile, and arbitration qualifiers). For Pointers and references, the result references the original object. For a pointer to a data member, the result will refer to the same member as the original (uncast) pointer to the data member. Depending on the type of reference object, an undefined behavior may occur through the generated pointer, a reference, or a write to a pointer to a data member.
You cannot directly override the constant state of a constant variable using the const_cast operator.
The const_cast operator converts the null pointer value to the null pointer value of the target type.


// expre_const_cast_Operator.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class CCTest {
public:
  void setNumber( int );
  void printNumber() const;
private:
  int number;
};

void CCTest::setNumber( int num ) { number = num; }

void CCTest::printNumber() const {
  cout << "\nBefore: " << number;
  const_cast< CCTest * >( this )->number--;
  cout << "\nAfter: " << number;
}

int main() {
  CCTest X;
  X.setNumber( 8 );
  X.printNumber();
}

In a row containing const_cast, the data type of the this pointer is const CCTest *. The const_cast operator changes the data type of the this pointer to CCTest * to allow modification of the member number. The cast only lasts for the rest of the statement in which it is in.

reinterpret_cast operator
allows you to convert any pointer to any other pointer type. It also allows you to cast any integer type to any pointer type and vice versa.
grammar


reinterpret_cast < type-id > ( expression )

note

Abusing the reinterpret_cast operator can be risky. Other cast operators 1 should be used unless the desired transformation is itself a low level. The reinterpret_cast operator can be used for transformations such as char* to int* or One_class* to Unrelated_class*, which is not itself secure. The result of reinterpret_cast cannot be safely used for any purpose other than casting back to its original type. At best, other USES are not portable. The reinterpret_cast operator cannot discard the const, volatile, or s 68en features. For more information about removing these features, see const_cast Operator. The reinterpret_cast operator converts the null pointer value to the null pointer value of the target type. One practical use of reinterpret_cast is in the hash function, that is, by having two different values map to an index in such a way that they almost never end up with the same index.

#include <iostream>

using namespace std;

// Returns a hash code based on an address
unsigned short Hash( void *p ) {
  unsigned int val = reinterpret_cast<unsigned int>( p );
  return ( unsigned short )( val ^ (val >> 16));
}

using namespace std;
int main() {
  int a[20];
  for ( int i = 0; i < 20; i++ )
   cout << Hash( a + i ) << endl;
}

Output:


64641
64645
64889
64893
64881
64885
64873
64877
64865
64869
64857
64861
64849
64853
64841
64845
64833
64837
64825
64829

reinterpret_cast allows Pointers to be treated as integer types. The result is then shifted bitwise and xor with itself to generate a 1-only index (with a very high probability of 1-only). This index is then truncated by the standard C style cast to the return type of the function.


Related articles: