#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Object;
class Memento
{
string name;
public:
Memento(string str):name(str){}
friend class Object;
};
class Object
{
string name;
public:
void Setname(string str){name = str;}
Memento* CreateMemento(){return new Memento(name);}
void Print(){cout<<name<<endl;}
void RestoreFromMemento(Memento* m){name = m->name;}
};
int main(int argc, char *argv[])
{
Object obj;
obj.Setname("old");
obj.Print();
Memento* pm = obj.CreateMemento();
obj.Setname("new");
obj.Print();
obj.RestoreFromMemento(pm);
obj.Print();
system("PAUSE");
return EXIT_SUCCESS;
}
Memento
最新推荐文章于 2023-11-13 11:23:43 发布