How to execute a piece of code only once?

后端 未结 8 790
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 04:35

I have an application which has several functions in it. Each function can be called many times based on user input. However I need to execute a small segment of the code wi

8条回答
  •  有刺的猬
    2020-12-01 05:18

    You can use local static variable:

    void foo()
    {
         static bool wasExecuted = false;
         if (wasExecuted)
             return;
         wasExecuted = true;
    
         ...
    }
    

提交回复
热议问题