枚举类的重载演员 [英] Overloading cast operator for enum class

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

问题描述

在我的项目中,我使用了多个枚举类,需要根据需要在它们之间轻松地进行铸造。它们基本上描述了同一件事,但命名不同,以使代码更易于使用。以下是枚举类:

In my project, I'm using multiple enum classes which I need to easily cast between depending on where I need to use them. They basically describe the same thing but are named differently to keep the code more easier to work with. Here are the enum classes:

enum class tetroType {
    None, I, O, T, J, L, S, Z
};

enum class gridBlock {
    Empty, Blue, Green, Orange, Purple, Red, Teal, Yellow
};

tetroType中的每个值都与gridBlock中的一个值相对应(fe tetroType :: I = gridBlock :: Teal ),但第一个保存有关tetronimo形状的信息(在tetronimo类中),第二个保存有关块的颜色的信息(在网格类中)。我知道我可以只使用一个枚举,但是这样您就不会丢失任何信息。另外,如果可能的话,我需要将其转换为字符串。我想这样使用它:

Each value in tetroType corresponds to a value in gridBlock (f. e. tetroType::I = gridBlock::Teal), but the first holds the information about the shape of tetronimo (in tetronimo class) and the second holds information about color of blocks (in grid class). I know I could just use one enum instead but this way you don't loose any information. Also I'd need to cast it into string if possible. This is how I would like to use it:

gridBlock grid = (gridBlock)tetroType::I;
string texture = (string)grid;

现在,我的设置方法是这样的。每当需要将一个枚举转换为另一个枚举或转换为字符串时,我都会在其他方法的中间使用此开关:

Right now the way I've got it set up is like this. I'm using this switch in the middle of other methods whenever I need to convert one enum into another or into a string:

switch (type) {
case tetroType::I:
    block = gridBlock::Teal;
    break;
case tetroType::O:
    block = gridBlock::Yellow;
    break;
case tetroType::T:
    block = gridBlock::Purple;
    break;
case tetroType::J:
    block = gridBlock::Blue;
    break;
case tetroType::L:
    block = gridBlock::Orange;
    break;
case tetroType::S:
    block = gridBlock::Green;
    break;
case tetroType::Z:
    block = gridBlock::Red;
    break;
case tetroType::None:
    block = gridBlock::Empty;
}


推荐答案

您应该考虑重载枚举类为整数(这是C ++ 11功能)。

You should look into overloading the enum class as an integer (this is a C++ 11 feature).

enum class tetroType : int {
    I = 1, O = 2, T = 3, J = 4, L = 5, S = 6, Z = 7, NONE
};

enum class gridBlock : int {
    Blue = 1, Green = 2, Orange = 3, Purple = 4, Red = 5, Teal = 6, Yellow = 9, EMPTY
};

从这里您可以使用ether C样式类型转换或static_cast

From here you can write a basic conversion using ether C Style typecasting, or static_cast

gridBlock ConvertTetro(tetroType type){
    return static_cast<gridBlock>(static_cast<int>(type));
}

gridBlock ConvertTetro(tetroType type){
    return (gridBlock)((int)((type)));
}

这将使任何网格块匹配相同的四型,并且默认为gridBlock: :EMPTY,如果没有匹配的类型。如果需要,此功能应该很容易弄清楚如何进行其他选择。从这里开始,您需要在两个之间匹配int值。

this will match any grid block to equal tetrotypes, and will default to gridBlock::EMPTY if theres no matching type. This function should be pretty easy to figure out how to go the other way if needed. From here you need to match up the int values between the two.

您还可以使用

enum class enumName : char

只要两个基础类型

这篇关于枚举类的重载演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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