参考如何传递枚举? [英] How do you pass an enum by reference?

查看:161
本文介绍了参考如何传递枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有四个键的枚举,作为接口程序的输入,我想通过值传递枚举到接口函数,这已经变得很长。枚举是这样的:

 枚举MYKEYS {
W,S,O,L
};

还有一个布尔数组,我必须通过引用传递,这也有点棘手。

  bool key [4] = {false,false,false,false};有没有人知道正确的语法来将这两个参数作为参考在一个函数中,类似于:



  function(int& anintreference); 


解决方案

您不能通过枚举本身作为您的函数的参数! 枚举定义了一个新的类型,您可以创建一个这种类型的变量,这个变量可能采用枚举

  MYKEYS k = W; 

只有这样,才能通过引用通过 k 一些功能:

  function foo(MYKEYS& k_); 

关于你的第二个问题,因为你应该把数组看作是一系列 bool

 功能栏(bool * key,int length); 


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

这篇关于参考如何传递枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆