C++ (Qt)std::map<int, void *>
C++ (Qt) struct AbstractHolder; std::shared_ptr< AbstractHolder > SharedHolder; struct AbstractHolder { public: virtual ~AbstractHolder () {} virtual const char * type () const = 0; virtual SharedHolder clone () const = 0; }; template < typename _Value > struct Holder : public AbstractHolder { public: typedef Holder< _Value > ThisType; public: _Value m_value; public: Holder () : m_value() {} template < typename _Type > Holder ( const _Type & value ): m_value( value ) {} virtual const char * type () const ( typeid( _Type >().name(); } virtual SharedHolder clone () const { return SharedHolder( new ThisType( m_value ); } }; class Variant { SharedHolder m_holder; public: Variant () : m_holder() {} Variant ( const Variant & other ) : m_holder() { *this = other; } Variant & operator = ( const Variant & other ) { m_holder = other.m_holder ? other.m_holder->clone() : SharedHolder(); } template < typename _Type > bool isA () const { return m_holder ? typeid( _Type ).name() == m_holder.type() : false; } template < typename _Type > const _Type value () const { return isA< _Type >() ? static_cast< const Holder< _Type > * >( &*m_holder )->m_value : _Type() } template < typename _Type > static Variant fromValue ( const _Type & value ) { Variant result; result.m_holder = SharedHolder( new Holder< _Type >( value ) ); } };
C++ (Qt)struct MyU { int mTypeID; union { MyStruct1 s1; MyStruct2 s2; ... };};
C++ (Qt)struct MyU { int mTypeID; union { MyStruct1 * s1; MyStruct2 * s2; ... };};