我有这个类:C++映射一个变量地址?
class Texture
{
public:
//I need this variable in this format
float diffuseColor[3];
}
但我想打一个更简单的界面比处理“diffuseColor [0]”和这样的,是这样的:
myTexture.color.r = 1.0f; //this is diffuseColor[0]
所以我试图让作为外壳的工作方式与diffuseColor值的一类,像这样:
class Color
{
public:
float *r, *g, *b;
}
而且在我的Texture类:
class Texture
{
public:
Texture()
{
color.r = &diffuseColor[0];
color.g = &diffuseColor[1];
color.b = &diffuseColor[2];
}
Color color;
private:
float diffuseColor[3];
}
但它现在是这样的,我必须去参考的颜色值,如果我想使用它们:
(*myTexture.color.r) = 1.0f;
我怎样才能做到这一点,而不必去引用它,每次我想用它吗?
使用引用而不是指针。并将引用的初始化移动到'Color'构造函数中,而不是'Texture'中。或者,也许你可以使用访问函数(例如,'r()/ r(float)'作为getter/setters)并去掉'Color'子类。 – syam
@syam:引用而不是指针将不允许复制。 –
@MooingDuck啊对了,没有想到,我的坏(我几乎总是使用访问器这种东西,他们更清洁恕我直言)。 – syam