jweinst1
8/14/2017 - 6:06 AM

reinterpretcast.cpp

// Example program
#include <iostream>

//uses reinterpret_cast<> to mimic c style casting 

#define Base_HANDLE int type; \
               struct Base* next; \
               struct Base* prev; 

struct Base
{
   Base_HANDLE 
};

struct IntObj
{
    Base_HANDLE
    long value;
};

void printType(Base* b)
{
    std::cout << b->type << std::endl;
}

void connect(Base* b1, Base* b2)
{
    b1->next = b2;
    b2->prev = b1;
}



int main()
{
    IntObj* i = new IntObj();
    i->type = 3;
    i->value = 500;
    IntObj* j = new IntObj();
    j->type = 3;
    j->value = 500;
    Base* b = reinterpret_cast<Base*>(i);
    std::cout << b->type << std::endl;
    printType(b);
    //3
    //3
    connect(reinterpret_cast<Base*>(i), reinterpret_cast<Base*>(j));
    std::cout << (reinterpret_cast<IntObj*>(i->next))->value << std::endl;
    //500
    delete i;
    delete j;
}