Reputation: 1049
I'm trying to pass an enum declared outside of a function's scope, to a function, so that changing the value of the enum within the function changes the value of the enum pointed to. Using the enum itself just in the scope of the object is not an option as I wish to use this function with multiple different instances of this enum.
I have an enum 'ColourState'. Declared like so. and a pointer to a ColourState.
enum ColourState {COLOUR1, COLOUR2};
ColourState* CS_GO_score;
The pointer was initialised like so.
CS_GO_score = new ColourState;
*CS_GO_score = ColourState::COLOUR2;
I'm now trying to pass the ColourState pointed to by CSS_GO_score to the function 'pulsateColour'
Like so.
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
}
}
}
}
This function is called like so.
pulsateColour(white,blue, GO_scoreColour, *CS_GO_score, 10);
However, when the value pointer to by 'goingTo' and 'CS_GO_score' (they should be the same address therefore technically the same object right?), watching the values monitor in VS i see that only the value pointed to by 'goingTo' (as it is local to the function) is changed? What am I doing wrong?
Upvotes: 4
Views: 22045
Reputation: 3571
However, when the value pointer to by 'goingTo' and 'CS_GO_score' (they should be the same address therefore technically the same object right?),
NO... It will be the "same object" if you pass the pointer itself. Here you are passing the value pointed by the pointer. Alternatively you may want to pass a reference.
void HUD::pulsateColour(GLfloat *colour1,
GLfloat *colour2,
GLfloat *objectCol,
ColourState goingTo, /* pass by value */
int timeInFrames)
...
I'm trying to pass an enum declared outside of a function's scope, to a function, so that changing the value of the enum within the function changes the value of the enum pointed to. You will need to change the funcion to take a pointer or a reference:
ColourState *goingTo, /* pass direct the pointer */
or
ColourState &goingTo, /* pass a reference-dont need to change the body of the function */
Upvotes: 1
Reputation: 227390
You are passing the enum by value, so the function has a local copy of whatever you pass it. If you want to pass an enum and modify it value, I suggest passing by reference.
enum ColourState {COLOUR1, COLOUR2};
void change_colour(ColourState& c)
{
c = COLOUR2;
}
....
ColourState cs = COLOUR1;
change_colour(cs);
Upvotes: 7