C++ union array and vars?

前端 未结 8 1468
日久生厌
日久生厌 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:38

    Depending on what "Scalar" is, yes, you can do that in C++. The syntax is almost exactly (maybe even exactly exactly, but I'm rusty on unions) what you wrote in your example. It's the same as C, except there are restrictions on the types that can be in the unions (IIRC they must have a default constructor). Here's the relevant Wikipedia article.

提交回复
热议问题