c++ - How to copy anonymous union -
i have anonymous union in struct
struct { union { char c; double d; }; a(const &a) { c = a.c; d = a.d; } };
i wondering best way define copy constructor. have copy union fields? guess have ...?
remember 1 union field @ time can active. copy union, need copy whichever 1 of them active.
generally, when using unions this, you'll have sort of "tag" field keeping track of field active. can use switch
statement on tag determine field active 1 , how copy over.
alternatively, consider using boost::variant
, type-safe wrapper around object of 1 of fixed number of types.
Comments
Post a Comment