I'm surprised this doesn't work:
union DlDatum
{
float mFloat;
s32 mInteger;
};
class DlDbE
{
public:
DlDbE( float f ) : mData.mFloat( f ) {};
private:
DlDatum mData;
};
Is there a way to initialize a union in a c++ constructor mem-initializer list?
Update: Answer is to create constructors for union. Didn't know that could be done. Here is what I did:
union DlDatum
{
float mFloat;
s32 mInteger;
bool mBoolean;
u32 mSymbol;
u32 mObjIdx;
DlDatum( ) : mInteger( 0 ) {}
DlDatum( float f ) : mFloat( f ) {}
DlDatum( s32 i ) : mInteger( i ) {}
DlDatum( bool b ) : mBoolean( b ) {}
DlDatum( u32 s ) : mSymbol( s ) {} // this ctor should work for objIdx also
};
class DlDbE
{
public:
DlDbE() {}
DlDbE( float f ) : mData( f ) {}
DlDbE( u32 i ) : mData( i ) {}
DlDbE( bool b ) : mData( b ) {}
...etc..
private:
DlDatum mData;
};
See Question&Answers more detail:os