2013-02-18 95 views
2

我試圖將函數範圍外聲明的枚舉傳遞給函數,以便更改函數內的枚舉值可以更改指向的枚舉值。僅僅在對象範圍內使用枚舉本身不是一個選項,因爲我希望將此函數與此枚舉的多個不同實例一起使用。傳遞枚舉函數的指針

我有一個enum'ColourState'。聲明如此。和一個指向ColourState的指針。

enum ColourState {COLOUR1, COLOUR2}; 
ColourState* CS_GO_score; 

指針初始化像這樣。

CS_GO_score = new ColourState; 
*CS_GO_score = ColourState::COLOUR2; 

我現在想通過ColourState指向CSS_GO_score給函數「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); 

然而,當值指針到「打算去」和「CS_GO_score」(他們應該是相同的地址,因此在技術上同一個對象吧?),看值VS監視我看到只有值'goingTo'指向(因爲它是本地的功能)改變了?我究竟做錯了什麼?

+0

所以你是一個反恐精英傢伙;)感謝您的問題,幫助我! – 2016-10-10 09:34:03

回答

5

您正在通過值傳遞枚舉,所以函數會傳遞它的本地副本。如果你想傳遞一個枚舉並修改它的值,我建議通過參考。

enum ColourState {COLOUR1, COLOUR2}; 

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

.... 

ColourState cs = COLOUR1; 
change_colour(cs); 
+0

再一次,你的網絡聖人已經爲我而來。 謝謝! – 2013-02-18 22:53:05

1

然而,當值指針到 '打算去' 和 'CS_GO_score' (它們應該是相同的,因此地址技術上相同的對象 正確?),

NO。 .. 如果你傳遞指針本身,它將是「相同的對象」。在這裏你傳遞指針指向的值。或者,您可能想要傳遞參考。

void HUD::pulsateColour(GLfloat *colour1, 
         GLfloat *colour2, 
         GLfloat *objectCol, 
         ColourState goingTo, /* pass by value */ 
         int timeInFrames) 

...

我想傳遞一個函數的範圍之外聲明的枚舉,以 功能,使函數內改變枚舉值 改變枚舉的值指向。 您將需要更改funcion採取指針或引用:

ColourState *goingTo, /* pass direct the pointer */ 

ColourState &goingTo, /* pass a reference-dont need to change the body of the function */ 
+0

我試着通過指針本身,而改變函數接受一個指針,而不是一個值。然而,在函數內部,我被告知賦值和布爾運算符(=和==)對於更改或比較值無效。例如 '* CS_GO_score == ColourState :: COLOUR_2'。 但是,通過引用已經奏效。謝謝。 – 2013-02-18 22:52:17

+0

你的意思是:* goingTo == ColourState :: COLOUR_2是一個錯誤,當函數使用ColourState * goingTo時,? – qPCR4vir 2013-02-18 23:01:19