C++ xxx_cast implements conversion code instance parsing

  • 2020-10-23 21:05:52
  • OfStack

1.1 static_cast

static_cast can implement implicit conversion in one direction and static conversion in another direction. It is suitable for both single and double hidden cases.

Double hidden

Double implicit means that both sides can be directly implicit conversion, suitable for 1 type of data conversion (such as int, float, double, long and other data types conversion)

Single hidden

Implicit conversion can only be performed in one direction and static conversion can only be achieved in the other direction. (For example, conversion between void* and Pointers, a pointer of any type can be converted to void*, but void* cannot be converted to a pointer of any type, so converting void* to a pointer of any type requires a call to a static conversion)


// The first thing to check is this static_cast , which can be realized in 1 Implicit conversion in one direction and another 1 The static conversion in four directions can be applied to single and double hidden cases 

  // The first is the double implicit, which means that you can implicitly convert both sides directly, 1 Generally applicable to basic data types, such as 
  int a = 4;
  double b = 3.2;
  a = b;
  b = a;
  cout << a << endl;
  cout << b << endl;
  a = static_cast<int> (b);
  b = static_cast<double> (a);

  // And then monosensitive, that is, only from 1 Through to the other 1 The edge is converted implicitly 
  // Pointers of any type can be converted to void*, but void* Cannot be converted to a pointer of any type 
  void* p = &b;
  int* q = &a;
  p = q;
  q = static_cast<int*>(p);

1.2 reinterpret_cast

reinterpret_cast "typically provides a lower-level reinterpretation of the bit patterns of operands" -- > That is to say, the data will be reinterpreted in base 2, in both upward can not be implicitly cast, it needs to be remastered. Double non - hidden case can be realized, such as int to rotate the pointer, pointer to rotate int, etc.


// Double not concealed 
  int *m=&a;
  int n=4;
  m = reinterpret_cast<int*>(n);
  n = reinterpret_cast<int>(m);

1.3 const_cast

Const_cast can be used to move unless the reference or pointer to an const object is constant. It can convert const variables to non-ES48en variables. It can be used to remove Pointers and references to const, and const_cast is a semantic complement to const. Its target type can only be a reference or pointer.

Non-const object -- > const reference or pointer -- > Take off your const - > Modify non-const objects


//const_cast--> Used to go unless const The object's const For Pointers and references 
  /************  The first 1 Cases, to quote const the  ************/
  int aa;
  const int& ra = aa;
  aa = 100;
  cout << aa << endl;
  cout << ra << endl;
  //ra = 200;// That would be wrong because ra is const , to achieve ra The modification, must go const the 
  const_cast<int&> (ra) = 300;
  cout << aa << endl;
  cout << ra << endl;

  /************  The first 2 In that case, go to the pointer const the  ************/
  const int* pp = &a;
  //*p = 200;// This is an error because of the pointer p is const Type, to implement p The modification, must go const the 
  *const_cast<int*>(pp) = 500;
  cout << *pp << endl;

Related articles: