English 中文(简体)
给使用工会的阶层成员变量分配同样的记忆
原标题:Assigning same memory to class member variables using unions

我正试图将现有的病媒阶层传入病媒。

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

设法使本类成员成为病媒,同时又不影响其他类别成员可变

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

但是,由于没有X、Y、Z号字眼,汇编错误。 是否有其他办法实现变量?

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

问题回答
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更大,要求你执行转让经营人的任务。

在标准C++中,没有办法使用工会。 你只能读一下你以前写过的话。 http://code>X,Y,Z, 后读vec4 产生未界定的行为。

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.

你可以 do切地做你们想要的东西(利用你的法典中的辛子)。

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;
};

但是,你通过<代码>v.X获取价值。

这应当做到。

#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();
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签