【问题标题】:C++ - basic function questionC++ - 基本函数问题
【发布时间】:2010-08-01 02:32:58
【问题描述】:

有没有办法只调用一次函数?

假设我有一些课程

struct A {

   void MainRoutine(Params) {
      // Want to call other routine only once
   }

   void OtherRoutine(Params) {
      // Do something that should be done only once and
      // what depends on the params
   }
};   

我只想在MainRoutine 中调用OtherRoutine 一次(我假设MainRoutine 将被调用N 次。我不能从构造函数调用OtherRoutine,因为它接受@987654327 @ 在构造对象时可能不可用。

基本上我想做类似的事情

static bool called = false;
if (!called) {
   OtherRoutine(Params);
   called = true;
}

但我希望有一种更“漂亮”的方式来做到这一点……(可以写成一行)

也许使用boost::functionboost 的某些部分我不知道? :)

谢谢

【问题讨论】:

  • 一行?请改用static const bool dummy = (OtherRoutine(params), true);。 ;)
  • @Georg 好吧,我实际上是在想一些 OBVIOUS 行,但这也很酷)

标签: c++ function call


【解决方案1】:

看看Boost Thread的one-time initialization mechanism

【讨论】:

    【解决方案2】:

    您还可以将您已经概述的仅调用一次逻辑放在OtherRoutine 中,如果之前已经执行过,则使其提前返回。

    从逻辑上讲,它几乎相同。在风格上,它可能会更好。

    【讨论】:

    • 标准库/boost不是有call_function_once这样的例程吗?我认为这可能有点用......无论如何,你的回答很好
    【解决方案3】:

    你肯定已经走在正确的轨道上了。你应该把你的静态“调用”变量放在你的结构中......咳咳:你应该把它变成一个类,让它成为私有的,并确保在 OtherRoutine 中查询静态变量的状态。 你不应该让它变得比它需要的更复杂。使用 boost 或其他任何东西来实现如此简单的机制只是矫枉过正。

    【讨论】:

      【解决方案4】:

      你可以通过 boost::function 和 bind 来实现。假设您希望每个对象只调用一次 OtherRoutine,

      struct A {
          A() {
              Routine = boost::bind(&A::OtherRoutine, this); 
          }
      
          boost::function<void()> Routine;
      
      private:
          void MainRoutine() {
              // Do stuff that should occur on every call
          }
      
          void OtherRoutine() {
              Routine = boost::bind(&A::MainRoutine, this);
              // Do stuff that should only occur once
              MainRoutine();
          }
      };
      
      A foo;
      foo.Routine(); // OtherRoutine is called
      foo.Routine(); // Now all subsequent calls will go to MainRoutine
      foo.Routine();
      

      不过,我建议按照其他人所说的去做。虽然这可能看起来“更干净”,但与其他替代方案相比,它过于复杂。

      【讨论】:

        【解决方案5】:

        另一种接近“可爱”的方式是拥有一个静态对象并从其构造函数中调用您的函数。比如……

           struct OneShotOtherRoutine
           {
              OneShotOtherRoutine(A a, Params params)
              {
                 a.OtherRoutine(params);
              }
           };
        
           struct A
           {
              friend struct OneShotOtherRoutine;
              public:
                 void MainRoutine(Params params)
                 {
                      static OneShotOtherRoutine(params);
                      // Main Routine code
                 }
              private:
                 void OtherRoutine(Params params)
                 {
                   // Other routine code
                 }
          };
        

        您必须将事情分开,以便每个实现都可以看到另一个结构的声明,但这可以做您想做的事情,假设在初始化静态时调用 OtherRoutine 是可以接受的。

        【讨论】:

          猜你喜欢
          • 2011-03-13
          • 2011-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-19
          • 2010-12-29
          • 1970-01-01
          相关资源
          最近更新 更多