3

我正在尝试将在函数范围之外声明的枚举传递给函数,以便更改函数内枚举的值会更改指向的枚举的值。仅在对象范围内使用枚举本身不是一个选项,因为我希望将此函数与此枚举的多个不同实例一起使用。

我有一个枚举'ColourState'。如此宣告。和一个指向 ColorState 的指针。

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”指向的值(因为它是函数的局部变量)被改变了?我究竟做错了什么?

4

2 回答 2

7

您正在按值传递枚举,因此该函数具有您传递的任何内容的本地副本。如果你想传递一个枚举并修改它的值,我建议通过引用传递。

enum ColourState {COLOUR1, COLOUR2};

void change_colour(ColourState& c)
{
  c = COLOUR2;
}

....

ColourState cs = COLOUR1;
change_colour(cs);
于 2013-02-18T22:35:32.003 回答
1

但是,当 'goingTo' 和 'CS_GO_score' 指向的值指针(它们应该是相同的地址,因此技术上是相同的对象对吗?),

不...如果您传递指针本身,它将是“相同的对象”。在这里,您正在传递指针指向的值。或者,您可能想要传递参考。

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 */
于 2013-02-18T22:37:31.493 回答