Union of the same type in C ++

Whenever I see examples of unification, they are always different. 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 it does not matter):

 union { float m_1stVar; float m_1stVarAlternateName; }; 

Whether it is good practice or not, will it cause any problems?

+6
source share
2 answers

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

+10
source

The problem arises only if you want to have unique values ​​for two variables. In your case, this should work fine.

0
source

Source: https://habr.com/ru/post/891591/


All Articles