C++ union array and vars?

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

    Try this:

    template
    struct U1
    {
        U1();
        T   v[2];
        T&  x;
        T&  y;
    };
    
    template
    U1::U1()
        :x(v[0])
        ,y(v[1])
    {}
    
    int main()
    {
        U1   data;
    
        data.x  = 1;
        data.y  = 2;
    }
    

提交回复
热议问题