简体   繁体   English

你如何通过引用传递枚举?

[英]How do you pass an enum by reference?

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);

You can't pass the enum itself as a parameter of your function! 您无法将enum本身作为函数的参数传递! 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 : enum定义了一个新类型,您可以创建此类型的变量,该变量可以采用enum定义的值之一:

MYKEYS k = W;

Only then you could pass k by reference to some function: 只有这样你才能通过参考某个函数来传递k

function foo(MYKEYS& k_);

Regarding your second question, since you should think of the array as a pointer to a series of bool : 关于你的第二个问题,因为你应该把数组看作指向一系列bool的指针:

function bar(bool* key, int length);

For the bool array, just take a bool* keys pointer. 对于bool数组,只需要一个bool* keys指针。 That works because arrays decay to pointers when passed to a function: 这是有效的,因为数组在传递给函数时会衰减为指针:

bool key[4] = {false, false, false, false};
void FuncThatTakesABoolArray(bool* keys){
    bool key1 = keys[0];
    // etc...
}

For the enum, Greg already answered that. 对于enum,Greg已经回答了这个问题。

Just use this way in your program: 只需在程序中使用这种方式:

foo(*new MYKEYS(MYKEYS::W));

And foo decleration/definition is like that: foo decleration / definition就是这样的:

void foo(MYKEYS &newMYKEYS){
    //
}

With enum you are defining a new type in your program. 使用枚举,您将在程序中定义一个新类型。 This type behaves the same way that any type. 此类型的行为与任何类型相同。 Then the pass by value is: 那么传递值是:

void myFunction( MYKEYS key );

And the pass by reference: 并通过参考传递:

void myFunction( MyKEYS& key );

If key will not be modified, better will be declare const reference: 如果不修改key,最好是声明const引用:

void myFunction( const MyKEYS& key );

Relative to a vector: 相对于矢量:

Vectors always are pass by reference in C++, and you don't need a pointer to do this. 向量总是在C ++中通过引用传递,并且您不需要指针来执行此操作。 The syntax is a bit different that normal types. 语法与普通类型略有不同。

void myFunction( bool keyVector [ ] );

Pass by reference is normally better than a pointer because someone can pass a NULL pointer to your function and break the program if you don't check the pointer previously. 通过引用传递通常比指针更好,因为如果您之前没有检查指针,有人可以将NULL指针传递给您的函数并中断程序。

In a matrix, ( a vector of vectors ) you must specify all dimensions except the first: 在矩阵中,(向量矢量)必须指定除第一个之外的所有维度:

bool keysOfKeys[4][3];

void myFunction( bool vector [ ] [ 3 ] );

There is a better form to do this without a vector of bools. 没有bool矢量,有一个更好的形式来做到这一点。

You can use the operator & ( bitwise and ) and ( | bitwise or ) and use each bit as a bool. 您可以使用运算符&(按位和)和(|按位或)并将每个位用作布尔值。 Remind that a enum behaves as a int. 提醒枚举表现为int。

For example, if the user has pressed the keys W and S. 例如,如果用户按下了键W和S.

enum MYKEYS
{
    W = 1<<0, // ..0001 ( binary ) 
    S = 1<<1, // ..0010 ( binary )
    O = 1<<2, // ..0100 ( binary )
    L = 1<<3 //  ..1000 ( binary )
};

MYKEYS k;
k = W | S;      // W and S pressed: ..0011 ( binary )

void myFunction( eKey key )
{
    // ..0011 & ..0001 -> ..0001
    if (  ( key  & W ) == W ); // key W pressed 

   // ..0011 & ..0010 -> ..0010
   if( ( key & S ) == S ); // key S pressed
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM