EDIT: corrected some compiler errors
Hi,
imagine a hierachy like this:
Code:
struct Base
{
int base1;
int base2;
};
struct Derived :public Base
{
int derived1;
int derived2;
};
now this would going on:
Code:
Derived d;
d.base1 = 5;
d.base2 = 6;
std::string d_str( reinterpret_cast<char*>(&d), sizeof(Derived) );
std::string b_str( d_str.c_str(), sizeof(Base) );
char* b_ch = new char( strlen(b_str.c_str()) +1 );
strcpy(b_ch, b_str.c_str());
//and now the interesting thing:
Base* b;
b = reinterpret_cast<Base*>(b_ch);
std::cout << b->base1 << " " << b->base2 << "\n";
Is b guaranteed to be a valid Base object, always?
If not, why?
Thank you!