开发者

C++ Function receiving an enum as one of its parameters

I am trying to make a function receive an enum as one of its parameters. I had the enum as a global but for some reason my other files couldn't change the enum. so I was wondering how do you set an enum as an argument for a function like,

function(enum AnEnum eee);

or is there a better way to solve the above problem?

Okay a quick 开发者_如何学Pythonrephrasing of my question: I basically have numerous files and I want all of them to have access to my enum and be able to change the state of that enum also the majority of files that should be able to access it are in a class. The way I was attempting to fix this was by passing the enum into the function that needed to access it, I couldn't work out how to go about making a function receive an enum as one of its arguments.


If you want to pass a variable that has a value of one of the enums values, this will do:

enum Ex{
  VAL_1 = 0,
  VAL_2,
  VAL_3
};

void foo(Ex e){
  switch(e){
  case VAL_1: ... break;
  case VAL_2: ... break;
  case VAL_3: ... break;
  }
}

int main(){
  foo(VAL_2);
}

If that's not what you mean, please clarify.


(1) my other files couldn't change the enum

You cannot change enum value as they are constants. I think you meant to change the enum variable value.

(2) how do you set an enum as an argument for a function ?

If you want to change the value of the enum variable then pass it by reference

void function (AnEnum &eee)
{
   eee = NEW_VALUE;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜