2017-01-09 97 views
-2

這裏是我的代碼:紀念品設計模式

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)()的意義是什麼?它是函數指針,那麼函數定義在哪裏?

+1

友好和善意的提示:把你的設計模式書籍,把他們藏在地下室。就在那隻憤怒的狗的籠子後面,它會咬人試圖去找他們的人。然後把它們忘掉,直到狗死了。同時,學習C++的基礎知識;) – BitTickler

+0

感謝您的建議@BitTickler將根據您的建議開始基礎知識:) –

回答

2

什麼是語句的意義:在上面的程序「typedef void(Number::*Action)()」 ?????它是函數指針,那麼函數定義在哪裏?

這是一個aliaspointer to a member-function。屬於這樣命名Number類和成員函數成員函數有什麼作爲參數,並返回一個void

這使得它可以使用名稱Action作爲類型說明符聲明一個變量,如前文所述,其類型。

ISO C++'s FAQ about Pointers to Members