This is piece of code from our team’s small project (acdemic)
class B{
public:
DWORD GetLen(){return i;}
DWORD i ;
};
class A
{
public:
DWORD GetLen(){return j;}
public:
int j ;
};
B b;
b.i = 2;
A * pA = (A *)&b;
int j = pA->GetLen();
“j” will be 2.
Is this code safe? Or, what should I modify it? By using reinterpret_cast or static_cast? or other thoughts?
No.
Remove the cast and only call member functions of
Aon instances ofA.As for what you should do instead, well, that depends entirely on what you’re actually trying to do.