在C ++中同类型的联合 [英] Union of same type in C++

查看:204
本文介绍了在C ++中同类型的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我看到联合的例子,他们总是不同的类型。例如,从MSDN:

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?

推荐答案

不,这不会导致任何问题。你没有看到它的更多的原因是,它是没有意义 - 两个名称指向相同的类型相同的值。

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.

这篇关于在C ++中同类型的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆