I everybody!
As the title says I'm having some problem trying to understand some pieces of code inside this class.
I found this code in a book, which explains how the class works, but doesn't tell anything about syntax (in fact, it's not a C++ guide).
Here's the code:
I'm not sure if about the usage of the union, enum and struct here.Code:-------- .h file --------- template <typename TAG> class Handle { union { enum { //Size to use for bit fields MAX_BITS_INDEX = 16; MAX_BITS_MAGIC = 16; //Size to compare against for asserting dereferences MAX_INDEX = ( 1 << MAX_BITS_INDEX ) - 1 ; MAX_MAGIC = ( 1 << MAX_BITS_MAGIC ) - 1 ; }; struct { unsigned m_Index : MAX_BITS_INDEX; //Index into resource array unsigned m_Magic : MAX_BITS_MAGIC; //Magic number to check }; unsigned int m_Handle; }; public: Handle(void) : m_Handle(0); void Init(unsigned int index); TUint GetIndex (void) const { return( m_Index ); } TUint GetMagic (void) const { return( m_Magic ); } TUint GetHandle (void) const { return( m_Handle ); } bool isNull (void) const { return( !m_Handle ); } operator unsigned int ( void ) vonst { return (m_Handle) } }; -------- .cpp file --------- template <typename TAG> void Handle <TAG> :: Init( unsigned int index ) { assert( IsNull() ); assert( index <= MAX_INDEX ); static TUint s_AutoMagic = 0; if( ++s_AutoMagic > MAX_MAGIC ) { s_AutoMagic = 1; } m_Index = index; m_Magic = s_AutoMagic; } template <typename TAG> inline bool operator != ( vyg_Handle <TAG> l, vyg_Handle<TAG> r ) { return ( l.GetHandle() != r.GetHandle() ); } template <typename TAG> inline bool operator == ( vyg_Handle <TAG> l, vyg_Handle<TAG> r ) { return ( l.GetHandle() == r.GetHandle() ); }
Starting from the union: it's anonymous (I know that) so this means that I can access its fields directly from a Handle object (ex: e.m_Handle).
Then, since it's a union, all its members will be stored starting from the same location, allocating a space as large as the largest member inside the union.
But here comes my question:
- how does the enum and struct are interpreted at compile time?
Furthermore when I compile it gives the following errors:
`m_Handle' undeclared (first use this function) vygHandle.h line 40
`m_Index' undeclared (first use this function) vygHandle.h line 38
`m_Magic' undeclared (first use this function) vygHandle.h line 39
ANSI C++ forbids in-class initialization of non-const static member `MAX_BITS_MAGIC' vygHandle.h line 17
ANSI C++ forbids in-class initialization of non-const static member `MAX_INDEX' vygHandle.h line 20
ANSI C++ forbids in-class initialization of non-const static member `MAX_MAGIC' vygHandle.h line 21
confused by earlier errors, bailing out vygHandle.h line 41
non-member function `GetHandle()' cannot have `const' method qualifier vygHandle.h line 40
non-member function `GetIndex()' cannot have `const' method qualifier vygHandle.h line 38
non-member function `GetMagic()' cannot have `const' method qualifier vygHandle.h line 39
non-member function `isNull()' cannot have `const' method qualifier vygHandle.h line 41
parse error before `;' vygHandle.h line 16
parse error before `public' vygHandle.h line 34
Could someone please help me here??
Thx a lot!!
Tesctassa