【问题标题】:How to have two functions that call each other C++如何让两个函数相互调用 C++
【发布时间】:2013-01-30 07:34:51
【问题描述】:

我有 2 个这样的函数可以对 if 循环进行混淆:

void funcA(string str)
{
    size_t f = str.find("if");
    if(f!=string::npos)
    {
        funcB(str);        //obfuscate if-loop
    }
}

void funcB(string str)
{
     //obfuscate if loop
     funcA(body_of_if_loop);     //to check if there is a nested if-loop
}

如果我将funcB 放在funcA 之前,则funcA 将无法看到funcB,反之亦然。

在此不胜感激。

【问题讨论】:

  • 为什么大家都称它为 if 循环?绝对不涉及循环。
  • @chris 很好,它取代了 for 循环结构,不是吗?

标签: c++ mutual-recursion


【解决方案1】:

你想要的是forward declaration。在你的情况下:

void funcB(string str);

void funcA(string str)
{
    size_t f = str.find("if");
    if(f!=string::npos)
    {
        funcB(str);        //obfuscate if-loop
    }
}

void funcB(string str)
{
     //obfuscate if loop
     funcA(body_of_if_loop);     //to check if there is a nested if-loop
}

【讨论】:

    【解决方案2】:

    forward declaration 可以工作:

    void funcB(string str); 
    
    void funcA(string str)
    {
        size_t f = str.find("if");
        if(f!=string::npos)
        {
            funcB(str);        //obfuscate if-loop
        }
    }
    
    void funcB(string str)
    {
         //obfuscate if loop
         funcA(body_of_if_loop);     //to check if there is a nested if-loop
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 2015-01-19
      • 2012-12-08
      • 2011-03-23
      相关资源
      最近更新 更多