template<typename type>
class CData
{
public:
DWORD a;
DWORD b;
DWORD c;
type* m_Data() { return (type*)(this + 1); }
};
Note: Used old school cast to make my code more readable.

DWORD Data[] = { 0, 0, 0, 0 };
CData<type>* nulldata = (CData<type>*)&data;
type *wti = (type*)(((BYTE*)&data) + sizeof(CData<type>));

Are these cast safe?

class CData
{
public:
DWORD a;
DWORD b;
BYTE* m_Data() { return (BYTE*)(this + 1); }
}
DWORD Data[] = { 0, 0, 0 };
CData* nulldata = (CData*)&data;

Do I avoid structure padding if I do this? Is structure padding even a issue?

Re: cast array to structure by Alexander

Alexander
Sat Jul 23 20:07:21 CDT 2005

"mr.sir bossman" <mrsirbossman@discussions.microsoft.com> wrote in message
news:D4B8B883-9958-4C7D-96C2-36B2890FD68D@microsoft.com...
> template<typename type>
> class CData
> {
> public:
> DWORD a;
> DWORD b;
> DWORD c;
> type* m_Data() { return (type*)(this + 1); }
> };
> Note: Used old school cast to make my code more readable.
>
> DWORD Data[] = { 0, 0, 0, 0 };
> CData<type>* nulldata = (CData<type>*)&data;
> type *wti = (type*)(((BYTE*)&data) + sizeof(CData<type>));
>
> Are these cast safe?
>

These casts are safe, but very ugly. Why not use:

template<typename type>
class CData
{
public:
DWORD a;
DWORD b;
DWORD c;
type* m_Data() { return (type*)(this + 1); }
//>>>>
static CData const * GetZeroData()
{
static struct ZeroData
{
CData data;
type zero;
} const zero_data = {0};
return &zero_data;
}
};

CData<type>const* nulldata;
nulldata = CData<type>*::GetZeroData();
or:
nulldata = nulldata->GetZeroData();