使用联合将相同的内存分配给类成员变量

发布于 2024-10-28 21:10:47 字数 503 浏览 1 评论 0原文

我正在尝试对现有的 Vector 类进行矢量化

class Vector
{
 public:
   float X,Y,Z;
};

尝试在不影响访问这些成员变量的其他类的情况下对类成员进行矢量化

class Vector
{
 public:
   union{
      float X,Y,Z;
      vector float vec4;
   };
};

,但存在编译器错误,因为找不到成员名称 X、Y、Z。有没有其他方法来获取变量?

作为参考,向量浮点类型来自IBM™ Cell 宽带引擎™ 用于多核加速的软件开发套件V3.0

I am trying to vectorize existing Vector class

class Vector
{
 public:
   float X,Y,Z;
};

Trying to vectorize the class members without affecting other classes accessing the these member variable

class Vector
{
 public:
   union{
      float X,Y,Z;
      vector float vec4;
   };
};

But there is a compiler error as no memeber name X,Y,Z found. Is there a alternative way to get the variable?

For reference, the vector float type comes from the IBM™ Cell Broadband Engine™
Software Development Kit V3.0 for Multicore Acceleration
.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-11-04 21:10:47
struct VectorData {
  float X, Y, Z;
};

union VectorUnion {
  VectorData VecData;
  vector float vec4;
};


class Vector {
public:
  Vector() : X(u.VecData.X), Y(u.VecData.Y), Z(u.VecData.Z) {}
  float & X;
  float & Y;
  float & Z;
private:
  VectorUnion u;
};

这是我相信您可以使用标准 C++ 获得的最接近的结果 - 这使得 Vector 更大,并且需要您自己实现赋值运算符。

struct VectorData {
  float X, Y, Z;
};

union VectorUnion {
  VectorData VecData;
  vector float vec4;
};


class Vector {
public:
  Vector() : X(u.VecData.X), Y(u.VecData.Y), Z(u.VecData.Z) {}
  float & X;
  float & Y;
  float & Z;
private:
  VectorUnion u;
};

This is the closest I believe you can get with standard C++ - This makes Vector larger, and requires you to implement the assignment operator yourself.

温柔戏命师 2024-11-04 21:10:47

在标准 C++ 中使用 union 无法做到这一点。您只能阅读您之前写过的内容。因此,编写 XYZ 然后读取 vec4 会产生未定义的行为。

我建议创建一个成员函数vector float toVector() const,它将在需要时创建向量。或者您可以考虑定义一个成员运算符向量 float() const。

There is no way to do that in standard C++ using union. You may only read what you have previously written. So writing X, Y, Z and then reading vec4 yields undefined behavior.

I would suggest to create a member function vector float toVector() const that will create the vector when needed. Or you may consider defining a member operator vector float() const.

再见回来 2024-11-04 21:10:47

您无法完全按照您想要的方式进行操作(使用代码中的语法)。

一个正确的方法示例是:

union{
    float coords[4];
    vector float vec4;
};

然后您可以逐个元素地挑选。

或者:

union{
    struct {
        float X, Y, Z;
    } v;
    vector float vec4;
};

但是您可以通过 .vX 访问值

You can't do quite exactly what you want (using the syntax from your code).

One example correct way to do it:

union{
    float coords[4];
    vector float vec4;
};

And then you can pick off element by element.

Alternatively:

union{
    struct {
        float X, Y, Z;
    } v;
    vector float vec4;
};

But then you access values via .v.X

攒一口袋星星 2024-11-04 21:10:47

这应该可以解决问题。

#include <iostream>

using namespace std;

class Point
{
public:
    Point() { X = 0.0f; Y = 0.0f; Z = 0.0f; };
    ~Point() {};

    union
    {
        struct{
            float X; float Y; float Z;
        };
        float index[3];
    };

    float operator[](int it)
    {
        return index[it];
    };
};

void main()
{
    Point p;

    p.X = 1.0f;
    p.Y = 2.0f;
    p.Z = 3.0f;

    cout << p[0] << " " << p[1] << " " << p[2] << endl;

    p.index[0] = 4.0f;
    p.index[1] = 5.0f;
    p.index[2] = 6.0f;

    cout << p[0] << " " << p[1] << " " << p[2] << endl;

    cin.get();
}

This should do the trick.

#include <iostream>

using namespace std;

class Point
{
public:
    Point() { X = 0.0f; Y = 0.0f; Z = 0.0f; };
    ~Point() {};

    union
    {
        struct{
            float X; float Y; float Z;
        };
        float index[3];
    };

    float operator[](int it)
    {
        return index[it];
    };
};

void main()
{
    Point p;

    p.X = 1.0f;
    p.Y = 2.0f;
    p.Z = 3.0f;

    cout << p[0] << " " << p[1] << " " << p[2] << endl;

    p.index[0] = 4.0f;
    p.index[1] = 5.0f;
    p.index[2] = 6.0f;

    cout << p[0] << " " << p[1] << " " << p[2] << endl;

    cin.get();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文