c ++返回枚举的函数? [英] c++ Function to return an enum?

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

问题描述

所以我有一个名为paddleNS的名为paddleNS的名为paddle的内部paddleNS里面我有一个枚举称为color

So I have this namespace called paddleNS for the class called paddle, inside paddleNS I have an enum known as colour

namespace paddleNS
{
   enum COLOUR {WHITE = 0, RED = 1, PURPLE = 2, BLUE = 3, GREEN = 4, YELLOW = 5, ORANGE = 6};
}

class Paddle : public Entity
{
private:
    paddleNS::COLOUR colour;
public:
    void NextColour();
    void PreviousColour();
    void PaddleColour(paddleNS::COLOUR col) { colour = col; }
};

现在,我想知道的是如何创建一个函数,颜色目前也是有一个更简单的方法来返回它的文本形式,而不是一个值或者我更好只是使用开关来找出什么颜色是什么?

Now then, what I was wondering is how would I go about creating a function that will return what the colour currently is also is there an easier way to return it in text form instead of a value or am I better of just using a switch to figure out what the colour is?

推荐答案

只需按值返回枚举:

class Paddle : public Entity
{
  // as before...

  paddleNS::COLOUR currentColour() const { return colour; }
};

这篇关于c ++返回枚举的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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