Is it safe to assume that the fields of an object are always in order of their definition when that object is dinamicly allocated?
Example that worked for me:
struct Rectangle
{
int len;
int wid;
};
Rectangle* myRect = new Rectangle;
void* vPtr = myRect;
Will *(int*)(vPtr)
be always equivalent to myRect->len
and *(int*)(vPtr + sizeof(int))
equivalent to myRect->wid
?
Will the same thing happen for this case too?
class Button
{
Rectangle* frame;
Char* Text;
} *myButton;
void * vButton = myButton;
Will *(int*)((*Rectangle)(vButton))
be the field len
?
Because i don't want a big book as an answer i will ask the following question. When i have a more classes like this:
#define WND_BUTTON 1
#define WND_EDIT 2
class Button
{
private:
BYTE TypeWindow;
SMALL_RECT srFrame;
*Some other fields*
public:
Button();
*Just functions here*
}
class Edit
{
private:
BYTE TypeWindow;
SMALL_RECT srFrame;
/*Some other fields*/
public:
Edit();
/*Just functions here*/
}
typedef void* HANDLE;
Can i rely on this check ?
HANDLE hWindow = Create(...);
if (*(BYTE*)(hWindow) == WND_BUTTON)
std::cout<<"This is a button."<<std:endl;
if (*(BYTE*)(hWindow) == WND_EDIT)
std::cout<<"This is a text field."<<std:endl;