在C/C++中将标签作为参数

Ibr*_*pek 0 goto c++11

我们有一个标签:

LABEL:
    //Do something.
Run Code Online (Sandbox Code Playgroud)

我们有一个功能.我们希望传递LABEL作为此函数的参数(否则我们无法访问函数中的标签),并且在某些情况下我们想要跳转此标签.可能吗?

我举一个例子(伪代码)来澄清:

GameMenu: //This part will be executed when program runs
//Go in a loop and continue until user press to [ENTER] key

while(Game.running) //Main loop for game
{
    Game.setKey(GameMenu, [ESCAPE]); //If user press to [ESCAPE] jump into GameMenu
    //And some other stuff for game
}    
Run Code Online (Sandbox Code Playgroud)

Sim*_*ple 5

这听起来像一个XY问题.您可能需要一台状态机:

enum class State {
    menu,
    combat,
};

auto state = State::combat;
while (Game.running) {
    switch (state) {
    case State::combat:
        // Detect that Escape has been pressed (open menu).
        state = State::menu;
        break;

    case State::menu:
        // Detect that Escape has been pressed (close menu).
        state = State::combat;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)