简体   繁体   English

C ++函数问题

[英]C++ functions problem

Is there a way for functions to call each other ie 有没有一种方法可以互相调用,即

void menu()
{
some code here
play();
...
}

int play()
{
...
menu();
...
return 0;
}

Add the declaration of the second function at the top of your code file: 在代码文件的顶部添加第二个函数的声明:

int play();

void menu()
{
   // some code here
   play();
   // ...
}

int play()
{
   // ...
   menu();
   // ...
   return 0;
}

This is called a forward declaration , and it informs the compiler that an identifier will be declared later. 这称为前向声明 ,它通知编译器以后将声明一个标识符。
It is a way of denoting a function so that you can call it before you provide the complete definition. 这是一种表示函数的方法,以便您可以在提供完整定义之前对其进行调用。

是的,但这几乎从来都不是您想要做的,因为不小心使用会破坏堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM