Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass an enum by reference?

Tags:

c++

enums

I have an enum with four keys I'm taking as input for an interface program and I'd like to pass the enum by value to the interface function, which has become quite long. The enum is like this:

enum MYKEYS {
  W, S, O, L
};

There's also a boolean array that I have to pass by reference, which is also a little tricky.

bool key[4] = { false, false, false, false };

Does anyone know the proper syntax to pass both of these as reference in a function, similar to:

function(int & anintreference);
like image 969
Darkenor Avatar asked Dec 22 '22 17:12

Darkenor


1 Answers

You can't pass the enum itself as a parameter of your function! The enum defines a new type, and you can create a variable of this type, that may take one of the value defined by the enum:

MYKEYS k = W;

Only then you could pass k by reference to some function:

function foo(MYKEYS& k_);

Regarding your second question, since you should think of the array as a pointer to a series of bool:

function bar(bool* key, int length);
like image 54
Greg Avatar answered Jan 23 '23 06:01

Greg