-2

Here is my code:

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;
  }
}

What is the significance of the statement typedef void(Number:: *Action)() in above program? Is it function pointer then where is the function definition?

halfer
  • 19,824
  • 17
  • 99
  • 186
ssg
  • 247
  • 2
  • 15
  • 1
    Friendly and well-meaning tip: Take your design pattern books and hide them in the basement. Right behind that cage of that angry dog which will bite anyone who tries to get to them. Then forget about them until the dog is dead. In the meantime, learn the basics of C++ ;) – BitTickler Jan 09 '17 at 10:56
  • Thanks for the advice @BitTickler will start basics as per your advice:) – ssg Jan 09 '17 at 11:04

1 Answers1

2

What is the significance of the statement : "typedef void(Number::*Action)()" in above program????? Is it function pointer then where is the function defination???

It is an alias for a pointer to a member-function. That member function belonging to a class so named Number, and the member function takes nothing as parameter and returns a void

It makes it possible to use the name Action as the type specifier to declare a variable whose type is as described before.

See ISO C++'s FAQ about Pointers to Members

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68