概述
示例
memento.cpp
1 class Memento 2 { 3 string state; 4 //.. 5 public: 6 Memento(const string & s) : state(s) {} 7 string getState() const { return state; } 8 void setState(const string & s) { state = s; } 9 }; 10 11 class Originator 12 { 13 string state; 14 //.... 15 public: 16 Originator() {} 17 Memento createMomento() { 18 Memento m(state); 19 return m; 20 } 21 void setMomento(const Memento & m) { 22 state = m.getState(); 23 } 24 }; 25 26 int main() 27 { 28 Originator orginator; 29 30 //捕获对象状态,存储到备忘录 31 Memento mem = orginator.createMomento(); 32 33 //... 改变orginator状态 34 35 //从备忘录中恢复 36 orginator.setMomento(memento); 37 38 }
组合、类层次结构、含指针类
原文:https://www.cnblogs.com/cxc1357/p/12320222.html