Explain the usage of const_cast and reinterpret_cast operators in C + + _c language

Source: Internet
Author: User

Const_cast operator
removes the const, volatile, and __unaligned attributes from the class.
Grammar

Const_cast < 
Type-id
 > ( 
expression
 )

Note
Pointers to any object type, or pointers to data members, can be explicitly converted to exactly the same type (except for the const, volatile, and __unaligned qualifiers). For pointers and references, the result refers to the original object. For a pointer to a data member, the result refers to the same member as the original (not cast) pointer to the data member. Depending on the type of the reference object, a write operation that generates a pointer, reference, or pointer to a data member can result in undefined behavior.
You cannot use the const_cast operator to directly override the constant state of a constant variable.
The const_cast operator converts a null pointer value to a NULL pointer value of the destination 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::p rintnumber () const {
  cout << "\nbefore:" << number;
  const_cast< Cctest * > (this)->number--;
  cout << "\nafter:" << number;
}

int main () {
  cctest X;
  X.setnumber (8);
  X.printnumber ();
}

In the row containing const_cast, the data type of this pointer is const CCTEST *. The const_cast operator changes the data type of the this pointer to Cctest * to allow the member number to be modified. The cast persists only for the remainder of the statement in which it resides.

Reinterpret_cast operator
allows any pointer to be converted to any other pointer type. It also allows you to convert any integer type to any pointer type and reverse conversion.
Grammar

Reinterpret_cast < Type-id > (expression)

Note

    • Misuse of the reinterpret_cast operator can be a risk-prone. You should use one of the other cast operators unless the desired transformation itself is low-level.
    • The reinterpret_cast operator can be used for conversions such as char* to int* or one_class* to unrelated_class*, which is inherently unsafe.
    • The result of the reinterpret_cast is not safe for any purpose other than casting back to its original type. In the best case, other uses are not portable.
    • The reinterpret_cast operator cannot discard the const, volatile, or __unaligned attributes. For more information about removing these attributes, see Const_cast Operator.
    • The reinterpret_cast operator converts a null pointer value to a NULL pointer value of the destination type.
    • One practical use of reinterpret_cast is to map values to the index in a hash function, that is, by having two different values close to the end of the same index.
#include <iostream>

using namespace std;

Returns a hash code based on "a"
unsigned short hash (void *p) {
  unsigned int val = reinterpret_cast< ; unsigned int> (p);
  return (unsigned short) (val ^ (Val >>));
}

using namespace std;
int main () {
  int a[20];
  for (int i = 0; i < 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 produce a unique index (the probability of uniqueness is very high). The index is then truncated by the standard C-style cast to the return type of the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.