sha*_*hal 2 c++ enums enumeration
我如何组织枚举,以便每个枚举条目都有参数,例如我在Haxe中可以做的,例如:
enum GraphicAct {
ClearScreen;
MoveTo(x:Float, y:Float);
LineTo(x:Float, y:Float);
FillColor(color:Int);
EndFill;
}
function main(){
var actions:Array<GraphicAct>;
actions.push(ClearScreen);
actions.push(FillColor(0xaaffff));
actions.push(MoveTo(100, 100));
actions.push(LineTo(200, 100));
actions.push(LineTo(100, 200));
actions.push(EndFill);
for(act in actions){
switch(act){
case ClearScreen: // do clear screen here...
case MoveTo(x, y): // move position
case LineTo(x, y): // move position
}
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,C++仅支持没有参数的枚举条目,如"ClearScreen"和"EndFill",但在这种情况下,我如何在C++中组织命令序列,就像我在图形命令中的例子一样?
你可以用union和enum,是这样的:
enum class EGraphicActType
{
ClearScreen, MoveTo, LineTo, FillColorData, EnfFillData
};
struct ClearScreenData {};
struct MoveToData { float x; float y;};
struct LineToData { float x; float y;};
struct FillColorData { Int color;};
struct EnfFillData {};
struct GraphicAct {
EGraphicActType type;
union
{
ClearScreenData clearScreenData;
MoveToData moveToData;
LineToData lineToData;
FillColorData fillColorData;
EnfFillData endFillData;
} data;
};
Run Code Online (Sandbox Code Playgroud)
如果你有权使用提升,你可以使用boost::variant.