I need to create a function that undoes the previous task/addition/change. How do I do this in Borland C++?
(The program stores strings of text in a text file using "list". It is stored and then erased unless I use the save-function I've created.)
I meant creating an undo function in a simple console application by the way.
I'll give yet another answer, but I think that the coverage has been insufficient so far.
The subject is far from trivial, and googling it returns a good number of results. Many applications implement a "undo" operation, and there are many variants.
There are 2 design patterns which can help us out here:
Command
: it's a reification of an actionMemento
: which consists in storing state (usually implies some form of serialization)The Command
pattern is heavily used in graphic environments because there is usually various ways to accomplish an action. Think of save in Microsoft Word for example:
And of course save is probably implemented in term of save as.
The advantage of the Command
pattern here is twofold:
undo
operationNow, there are various issues proper to undo
:
rm
on Linux or the empty trash bin action on Windows)Because there are various problems, there are various strategies:
Memento
kick in)Memento
s which consumes space, you can then use an approach which consists in only memorizing one Snapshot every 10 or 20 commands, and then redoing the commands from the latest snapshot up to the undone commandIn fact, you can probably mix Command
and Memento
at leisure, depending on the specifics of your system and thus the complexity of either.
I would only considering undoing the last action executed to begin with (using a stack of action then). The functionality of undoing whatever action the user wishes is much more complicated.