C ++类的私有成员函子

时间:2013-05-19 05:34:55

标签: c++ class c++11 lambda functor

我正在编写一个类,我想要一些成员方法,这些方法有一些与之相关的数据,特别是他们需要使用的机器人系统。我以为我可以把它们写成仿函数,就像这样(这不是我的实际代码):

class MyRobot : public Robot {
public:
  MyRobot();
  void runRobot();
private:
  Command do_something_,
          do_another_thing_;
}

然后在构造函数中使用lambda初始化do_something_,如:

do_something_( [] {
  do_first_thing();
  do_second_thing();
} );

然后告诉do_something_它有什么要求:

do_something_.requires( system_a );
do_something_.requires( system_b );

runRobot()中,我会告诉机器人的调度程序执行命令:

void MyRobot::runRobot() {
  scheduler.add(do_something_);
  scheduler.add(do_another_thing_);
}

但我已经意识到,随着命令数量的增加,MyRobot的构造函数将变得越不易管理,因为每个命令都将在其中定义其主体。我可以为每个命令创建一个相应的私有方法,并用函数指针而不是lambda初始化它们,但这似乎更复杂。我还可以为每个特定命令创建子类Command,从而将主体和需求放在每个文件的单独文件中,但这对于一个相当简单的任务来说感觉就像很多开销。有没有一种方法可以做到这一点,我不知道?

1 个答案:

答案 0 :(得分:0)

您可以定义Command类以获取std::function和初始化列表“需求”。然后,您可以将do_somethingdo_another_thing设置为自己的私有成员函数,而不是使用lambdas,这样您就不必在构造函数中定义它们的主体。最后,在构造函数中,您可以通过将私有成员函数与当前MyRobot实例的this指针绑定来构造Command实例,同时还为它们提供需求列表。 Command对象应该能够修改MyRobot个实例的私有状态。一个例子如下。另请参阅example output

#include <functional>
#include <iostream>
#include <vector>

enum System { SYS_A, SYS_B, SYS_C };

class Command {
public:
  typedef std::function<void()> FuncType;

  Command(FuncType func, std::initializer_list<System> requirements)
  :func_(func), requirements_(requirements) { }

  void operator()() {
    std::cout << "Executing Command:" << std::endl;
    for ( System s : requirements_ )
      std::cout << "   REQUIRES " << static_cast<int>(s) << std::endl;
    func_();
  }

private:
  FuncType            func_;
  std::vector<System> requirements_;
};

class Scheduler {
public:
  void add(Command c) {
    c();
  }
};

class Robot {
public:
  Robot()
  :do_something_    (std::bind(&Robot::do_something, this),     {SYS_A, SYS_B}),
   do_another_thing_(std::bind(&Robot::do_another_thing, this), {SYS_A, SYS_C}) { }

  void runRobot() {
    s_.add(do_something_);
    s_.add(do_another_thing_);
  }

private:
  void do_first_thing()  { std::cout << "   FIRST THING!" << std::endl;  }
  void do_second_thing() { std::cout << "   SECOND THING!" << std::endl; }
  void do_third_thing()  { std::cout << "   THIRD THING!" << std::endl;  }

  void do_something()     { do_first_thing(); do_second_thing(); }
  void do_another_thing() { do_first_thing(); do_third_thing();  }

  Command   do_something_;
  Command   do_another_thing_;
  Scheduler s_;
};

int main(int, char**) {
  Robot().runRobot();
}