我正在尝试将在函数范围之外声明的枚举传递给函数,以便更改函数内枚举的值会更改指向的枚举值。仅仅在对象范围内使用枚举本身不是一个选项,因为我希望将此函数与此枚举的多个不同实例一起使用。
我有一个枚举'ColourState'。宣布如此。和指向ColourState的指针。
enum ColourState {COLOUR1, COLOUR2};
ColourState* CS_GO_score;
指针初始化如此。
CS_GO_score = new ColourState;
*CS_GO_score = ColourState::COLOUR2;
我现在正试图将CSS_GO_score指向的ColourState传递给函数'pulsateColour'
喜欢这样。
void HUD::pulsateColour(GLfloat *colour1, GLfloat *colour2, GLfloat *objectCol, ColourState goingTo, int timeInFrames)
{
GLfloat difference[4];
//give us an array of values to change the array by in the alloted time
difference[0] = (colour1[0]-colour2[0])/timeInFrames;
difference[1] = (colour1[1]-colour2[1])/timeInFrames;
difference[2] = (colour1[2]-colour2[2])/timeInFrames;
difference[3] = (colour1[3]-colour2[3])/timeInFrames;
//depending on the state, transform the array in one direction or another
if(goingTo == ColourState::COLOUR2)//if we're moving toward colour 2
{
for(int i = 0; i<4; i++)
{
objectCol[i] -= difference[i];//subract the difference till we get there
//we need to SNAP to the colour as we will not hit it every time using floats
if( (objectCol[i]>(colour2[i]-(difference[i]*2))) && (objectCol[i]<(colour2[i]+(difference[i]*2))) )
{//if we hit this tiny but huuuge target
objectCol[i] = colour2[i];//SNAP
}
}
}else{
if(goingTo == ColourState::COLOUR1)
{
for(int i = 0; i<4; i++)
{
objectCol[i] += difference[i];//add the difference till we get there
//we need to SNAP to the colour as we will not hit it every time using floats
if( (objectCol[i]>(colour1[i]-(difference[i]*2))) || (objectCol[i]<(colour1[i]+(difference[i]*2))) )
{//if we hit this tiny but huuuge target
objectCol[i] = colour1[i];//SNAP
}
}
}
}
if((objectCol[0] == colour1[0])&&(objectCol[1] == colour1[1])&&(objectCol[2] == colour1[2])&&(objectCol[3] == colour1[3]))
{//if the objcolour == colour 1
goingTo = ColourState::COLOUR2;//it's now time to move towards colour 2
}else{
if((objectCol[0] == colour2[0])&&(objectCol[1] == colour2[1])&&(objectCol[2] == colour2[2])&&(objectCol[3] == colour2[3]))
{//if the objcolour == colour 2
goingTo = ColourState::COLOUR1;//it's now time to move towards colour1
}
}
}
}
这个函数就是这样调用的。
pulsateColour(white,blue, GO_scoreColour, *CS_GO_score, 10);
然而,当指向'goingTo'和'CS_GO_score'的值时(它们应该是相同的地址因此在技术上相同的对象对吗?),观看VS中的值监视器我看到只有'指向的值' goingTo'(因为它是函数的本地)是改变了吗?我做错了什么?
答案 0 :(得分:5)
您正在按值传递枚举,因此该函数具有您传递的任何内容的本地副本。如果你想传递枚举并修改它的值,我建议通过引用传递。
enum ColourState {COLOUR1, COLOUR2};
void change_colour(ColourState& c)
{
c = COLOUR2;
}
....
ColourState cs = COLOUR1;
change_colour(cs);
答案 1 :(得分:1)
然而,当指向'goingTo'和'CS_GO_score'的值时 (它们应该是相同的地址,因此在技术上是相同的对象 右?),
... NO 如果你传递指针本身,它将是“同一个对象”。在这里传递指针指向的值。或者,您可能想要传递参考。
void HUD::pulsateColour(GLfloat *colour1,
GLfloat *colour2,
GLfloat *objectCol,
ColourState goingTo, /* pass by value */
int timeInFrames)
...
我正在尝试将在函数范围之外声明的枚举传递给 一个函数,以便在函数内改变枚举的值 更改指向的枚举值。 您需要更改功能以获取指针或引用:
ColourState *goingTo, /* pass direct the pointer */
或
ColourState &goingTo, /* pass a reference-dont need to change the body of the function */