Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;以后再拿指令_C++_User Interface_Methods_Draw_Instructions - Fatal编程技术网

C++ c++;以后再拿指令

C++ c++;以后再拿指令,c++,user-interface,methods,draw,instructions,C++,User Interface,Methods,Draw,Instructions,我正在尝试设计一个UIDraw方法。我想在maindraw方法中声明要绘制的UI元素,但在代码后面有一个单独的UIDraw方法。所以我需要一种方法来存储要在这个新函数中执行的指令。我希望这是有道理的。 大概是这样的: Draw(); DrawUI(); 但是请在draw()函数中说出要绘制的UI。 关于如何解决这个问题有什么想法吗?根据您的具体需要,有很多方法可以解决这个问题。在OO世界中流行的一种方法是所谓的(类似的方法存在于其他编程范例中,它们要么有不同的名

我正在尝试设计一个UIDraw方法。我想在maindraw方法中声明要绘制的UI元素,但在代码后面有一个单独的UIDraw方法。所以我需要一种方法来存储要在这个新函数中执行的指令。我希望这是有道理的。 大概是这样的:

        Draw();
        DrawUI();
但是请在draw()函数中说出要绘制的UI。
关于如何解决这个问题有什么想法吗?

根据您的具体需要,有很多方法可以解决这个问题。在OO世界中流行的一种方法是所谓的(类似的方法存在于其他编程范例中,它们要么有不同的名称,要么被认为是非常明显的,甚至根本没有一个特定的名称)

基本思想是:您想要执行一些命令,但是您想要执行命令的时间和您决定执行什么命令的时间是不同的。因此,解决这个问题的方法是简单地创建一个包含执行命令所需信息的对象,将该对象传递到决定执行时间的位置,然后代码可以随意运行命令

这是一个在C++中可能看起来像什么的模型(注:实际上没有编译这个代码,可能包含小错误,只是为了传达这个想法)。


有很多方法可以解决这个问题,具体取决于你需要什么。在OO世界中流行的一种方法是所谓的(类似的方法存在于其他编程范例中,它们要么有不同的名称,要么被认为是非常明显的,甚至根本没有一个特定的名称)

基本思想是:您想要执行一些命令,但是您想要执行命令的时间和您决定执行什么命令的时间是不同的。因此,解决这个问题的方法是简单地创建一个包含执行命令所需信息的对象,将该对象传递到决定执行时间的位置,然后代码可以随意运行命令

这是一个在C++中可能看起来像什么的模型(注:实际上没有编译这个代码,可能包含小错误,只是为了传达这个想法)。


StackOverflow仅用于特定问题。请点击查看您可以在此处询问的内容。“关于如何解决此问题的任何想法?”可能会使用特定UI元素的可选参数?@Nikitademov该问题对我来说足够具体?可能会制作std::函数的std::vector,然后在向量上迭代并调用所有函数?StackOverflow仅用于特定问题。请点击查看您可以在此处询问的内容。“关于如何解决此问题的任何想法?”可能会使用特定UI元素的可选参数?@Nikitademov该问题对我来说已经足够具体了?可能会创建一个std::vector of std::functions,然后在该向量上迭代并调用所有函数?非常感谢。我是新的C++(用于统一),所以这对我来说都是新的。但这教会了我很多!非常感谢你!!非常感谢你。我是新的C++(用于统一),所以这对我来说都是新的。但这教会了我很多!非常感谢你!!
#include <memory>
#include <vector>

/// this is an abstract class that gives us an interface to use
class DrawCommand {
  public:
  virtual void Draw() = 0;
};

/// one kind of thing you might want to draw
class DrawTree : public DrawCommand {
  public:
  void Draw() override {
     // tree drawing code
  }
};

/// another kind of thing you might want to draw
class DrawCat : public DrawCommand {
  public:
  void Draw() override {
    // cat drawing code
  }
};

/// we can even come up with ways to combine these in interesting ways
class DrawABunchOfThings : public DrawCommand {
  std::vector<std::unique_ptr<DrawCommand>> things;
  public:
  DrawABunchOfThings(std::vector<std::unique_ptr<DrawCommand>> things)
    : things{std::move(things)}
  {}

  void Draw() override {
    for(auto &thing : things) {
      thing->Draw();
    }
  }
};

/// this is where we decide what we will draw
std::unique_ptr<DrawCommand> PrepareDraw() {
  if(someCondition) {
    // just a cat
    return std::make_unique<DrawCat>();
  } else if(someOtherCondition) {
    // just a tree
    return std::make_unique<DrawTree>();
  } else {
    // forest with a cat hidden inside
    return std::make_unique<DrawABunchOfThings>(
      std::vector<std::unique_ptr<DrawCommand>>{
        std::make_unique<DrawTree>(),
        std::make_unique<DrawTree>(),
        std::make_unique<DrawCat>()
        std::make_unique<DrawTree>(),
      }
    );
  }
}

/// this is where we will do the actual drawing
/// note that any arbitrary amount of code can go between
/// PrepareDraw and ExecuteDraw
void ExecuteDraw(DrawCommand &command) {
  // this can of course have a bunch of elaborate
  // code here as well -- also, DrawCommand::Draw might
  // take extra parameters here, like 2D or 3D transforms,
  // time since we last drew something, or whatever
  command.Draw();
}
int nTimes = 10;
DrawCommand drawNTimesCommand = [nTimes]() {
  for(int i = 0; i < nTimes; ++i) {
    // draw something
  }
};

// --- any code you like here ---

// actually execute the draw command
drawNTimesCommand();