Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

enums

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 rephrasing 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.

like image 305
I Phantasm I Avatar asked Jun 07 '11 05:06

I Phantasm I


1 Answers

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.

like image 65
Xeo Avatar answered Oct 26 '22 07:10

Xeo