In C++03 and before you are limited to writing a constructor for your union.
In C++11 the uniform initialization extents the syntax of aggregate initialization to constructor initializer lists. This means that the good old aggregate initializer syntax like
DlDatum d = { 3.0 };
which we all know and love from C and which initializes the first member of the union, can now be used in constructor initializer lists as well
union DlDatum
{
float mFloat;
s32 mInteger;
};
class DlDbE
{
public:
DlDbE( float f ) : mData{f} {}
private:
DlDatum mData;
};
This feature only allows you to "target" the first non-static member of the union for initialization. If you need something more flexible, then it is back to writing constructors.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…