Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union of same type in C++

Tags:

c++

unions

Whenever I see examples of union, they are always different types. For example, from MSDN:

// declaring_a_union.cpp
union DATATYPE    // Declare union type
{
    char   ch;
    int    i;
    long   l;
    float  f;
    double d;
} var1;          // Optional declaration of union variable

int main()
{
}

What happens if I have a union (in this case anonymous, but that shouldn't matter) like this:

union
{
    float m_1stVar;
    float m_1stVarAlternateName;
};

Regardless of whether this is good practice or not, will this cause any issues?

like image 787
Samaursa Avatar asked Feb 03 '23 16:02

Samaursa


1 Answers

No, this won't cause any issues. The reason you don't see it more often is that it's pointless - both names refer to the same value of the same type.

like image 143
Mark Ransom Avatar answered Feb 12 '23 13:02

Mark Ransom