Expanding struct/union (VARIANT exemple)

Hi guys,

I'm trying to understand how the struct/union of the VARIANT type is done. See
file <oaidl.h> for VARIANT definition.

I want to implement a more simple VARIANT type for my application, but I want the
same behavior, i.e. if my variable name is var (VARIANT var), I want to be able to
achieve both var.vt = VT_I4 and vt.iVal = 0.

My problem is that if I do a struct like this,

struct MYVARIANT
{
int vt;
union
{
int iVal;
long lVal;
double dVal;
} __MYVARIANT1;
};

var.vt will be visible but not iVal nor lVal nor dVal ... how can I acheive the same implementation
as the VARIANT type

Thanks,
P.A.



Answer this question

Expanding struct/union (VARIANT exemple)

  • Radith

    iVal and the rest are still visible, but through __MYVARIANT1. So, you should make the union anonymous.

  • TPECI

    You can use an unnamed union, such as

    struct MYVARIANT
    {
    int vt;
    union
    {
    int iVal;
    long lVal;
    double dVal;
    };
    };

    MYVARIANT v;
    v.iVal = 100;
    // ...



  • Expanding struct/union (VARIANT exemple)