C ++ - 通过调用main()函数重新启动游戏 [英] C++ - Restarting a game by calling the main() function

查看:306
本文介绍了C ++ - 通过调用main()函数重新启动游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个小游戏。
其中一个输入选项是重新启动游戏。我想到的唯一方法是从main函数中调用main函数。

I'm building a small game. One of the input options is to restart the game. The only way I could think of doing this was to call the main function from within the main function

int main(int argc, char argv[]) {
 ...
 if (input == "restart") {
  main(argc, argv);
 }

这是不良形式吗?

推荐答案

您不能调用 main()递归。这实际上是未定义的行为。

You can't call main() recursively. That's actually undefined behavior.

使用循环代替:

int main() {
     bool restart = false;
     do {
         // Do stuff ...

         // Set restart according some condition inside of the loop
         if(condition == true) {
             restart = true;
         } // (or simplyfied restart = condtion;)
     } while(restart);
}

这篇关于C ++ - 通过调用main()函数重新启动游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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