What is reinterpret_cast - C++ Data Type

C++ examples for Data Type:Cast

Introduction

A reinterpret_cast is to change entirely to different type of object.

Demo Code

#include <iostream>
using namespace std;
const int sz = 100;
struct X { int a[sz]; };
void print(X* x) {
   for(int i = 0; i < sz; i++)
      cout << x->a[i] << ' ';
   cout << endl;/*from  w w w. j a va  2s .  c o m*/
}
int main() {
   X x;
   print(&x);
   int* xp = reinterpret_cast<int*>(&x);
   for(int* i = xp; i < xp + sz; i++)
      *i = 0;
   print(reinterpret_cast<X*>(xp));
   print(&x);
}

Result


Related Tutorials