有没有可以在函数外跳转的函数?
Is there a function that can jump over outside of a function?
有没有像example: goto这样可以跳过函数的函数?假设我有这段代码:
#include <iostream>
void function1() {
//dothis1
//dothis2
//jump to other function
}
int main() {
std::cout<<"a";
//go to here (jump)
std::cout<<"b";
}
您可以直接调用您要跳转到的另一个函数,如下所示。在下面的程序中,我们从 main
内部调用 function1
,当 function1
完成时,控制权将自动返回给调用函数 main
.
然后我们从 function1
内部调用 dothis1
和 dothis2
并且当 dothis1
和 dothis2
完成控制将自动返回给调用函数 function1
.
void dothis1()
{
std::cout<<"entered dothis1"<<std::endl;
}
void dothis2()
{
std::cout<<"entered dothis2"<<std::endl;
}
void function1()
{
std::cout<<"entered function1"<<std::endl;
//call dothis1
dothis1();
//now call dothis2
dothis2();
}
int main()
{
//call function1
function1();
return 0;
}
上面程序的输出可见here:
entered function1
entered dothis1
entered dothis2
有没有像example: goto这样可以跳过函数的函数?假设我有这段代码:
#include <iostream>
void function1() {
//dothis1
//dothis2
//jump to other function
}
int main() {
std::cout<<"a";
//go to here (jump)
std::cout<<"b";
}
您可以直接调用您要跳转到的另一个函数,如下所示。在下面的程序中,我们从 main
内部调用 function1
,当 function1
完成时,控制权将自动返回给调用函数 main
.
然后我们从 function1
内部调用 dothis1
和 dothis2
并且当 dothis1
和 dothis2
完成控制将自动返回给调用函数 function1
.
void dothis1()
{
std::cout<<"entered dothis1"<<std::endl;
}
void dothis2()
{
std::cout<<"entered dothis2"<<std::endl;
}
void function1()
{
std::cout<<"entered function1"<<std::endl;
//call dothis1
dothis1();
//now call dothis2
dothis2();
}
int main()
{
//call function1
function1();
return 0;
}
上面程序的输出可见here:
entered function1
entered dothis1
entered dothis2