C++ union array and vars?

前端 未结 8 1602
日久生厌
日久生厌 2021-02-19 22:18

There\'s no way to do something like this, in C++ is there?

union {
    {
        Scalar x, y;
    }
    Scalar v[2];
};

Where x == v[0]<

相关标签:
8条回答
  • 2021-02-19 22:52

    With C++11 you have anonymous unions and structs which just export their definitions to the enclosing scope, so you can do this:

    typedef int Scalar;
    struct Vector
    {
        union
        {
            struct
            {
                Scalar x, y;
            };
            Scalar v[2];
        };
    };
    
    0 讨论(0)
  • 2021-02-19 22:54

    Since you are using C++ and not C, and since they are of the same types, why not just make x a reference to v[0] and y a reference to v[1]

    0 讨论(0)
提交回复
热议问题