【问题标题】:Memento design pattern纪念品设计模式
【发布时间】:2017-05-23 14:41:22
【问题描述】:

这是我的代码:

class Number;

class Memento
{
  public:
    Memento(int val)
    {
        _state = val;
    }
  private:
    friend class Number; // not essential, but p287 suggests this
    int _state;
};

class Number
{
  public:
    Number(int value)
    {
        _value = value;
    }
    void dubble()
    {
        _value = 2 * _value;
    }
    void half()
    {
        _value = _value / 2;
    }
    int getValue()
    {
        return _value;
    }
    Memento *createMemento()
    {
        return new Memento(_value);
    }
    void reinstateMemento(Memento *mem)
    {
        _value = mem->_state;
    }
  private:
    int _value;
};

class Command
{
  public:
    typedef void(Number:: *Action)();
    Command(Number *receiver, Action action)
    {
        _receiver = receiver;
        _action = action;
    }
    virtual void execute()
    {
        _mementoList[_numCommands] = _receiver->createMemento();
        _commandList[_numCommands] = this;
        if (_numCommands > _highWater)
          _highWater = _numCommands;
        _numCommands++;
        (_receiver-> *_action)();
    }
    static void undo()
    {
        if (_numCommands == 0)
        {
            cout << "*** Attempt to run off the end!! ***" << endl;
            return ;
        }
        _commandList[_numCommands - 1]->_receiver->reinstateMemento
          (_mementoList[_numCommands - 1]);
        _numCommands--;
    }
    void static redo()
    {
        if (_numCommands > _highWater)
        {
            cout << "*** Attempt to run off the end!! ***" << endl;
            return ;
        }
        (_commandList[_numCommands]->_receiver->*(_commandList[_numCommands]
          ->_action))();
        _numCommands++;
    }
  protected:
    Number *_receiver;
    Action _action;
    static Command *_commandList[20];
    static Memento *_mementoList[20];
    static int _numCommands;
    static int _highWater;
};

Command *Command::_commandList[];
Memento *Command::_mementoList[];
int Command::_numCommands = 0;
int Command::_highWater = 0;

int main()
{
  int i;
  cout << "Integer: ";
  cin >> i;
  Number *object = new Number(i);

  Command *commands[3];
  commands[1] = new Command(object, &Number::dubble);
  commands[2] = new Command(object, &Number::half);

  cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
  cin >> i;

  while (i)
  {
    if (i == 3)
      Command::undo();
    else if (i == 4)
      Command::redo();
    else
      commands[i]->execute();
    cout << "   " << object->getValue() << endl;
    cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
    cin >> i;
  }
}

上面程序中typedef void(Number:: *Action)()的声明有什么意义?是函数指针,那么函数定义在哪里?

【问题讨论】:

  • 友好而善意的提示:带上你的设计模式书,把它们藏在地下室里。就在那条愤怒的狗的笼子后面,它会咬任何试图接近它们的人。然后忘记他们,直到狗死了。同时,学习 C++ 的基础知识;)
  • 感谢您的建议@BitTickler 将根据您的建议开始基础知识:)

标签: c++ design-patterns memento


【解决方案1】:

上面程序中的语句:“typedef void(Number::*Action)()”有什么意义??????是函数指针,那么函数定义在哪里???

aliaspointer to a member-function。该成员函数属于一个名为 Number 的类,该成员函数不接受任何参数并返回 void

可以使用名称Action 作为类型说明符来声明类型如前所述的变量。

ISO C++'s FAQ about Pointers to Members

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2023-03-16
    • 1970-01-01
    • 2013-10-31
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多