Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Last active February 2, 2021 19:50
Show Gist options
  • Save Xenakios/e30a7c0be13808802a4d1fc5b930a8bb to your computer and use it in GitHub Desktop.
Save Xenakios/e30a7c0be13808802a4d1fc5b930a8bb to your computer and use it in GitHub Desktop.
class Undoable
{
public:
Undoable() {}
void setString(std::string str)
{
if (str.find("🍺") != std::string::npos)
{
std::cout << "No beer tonight! String not set.\n";
return;
}
oldString = currentString;
currentString = str;
}
std::string getString() const
{
return currentString;
}
void undo()
{
setString(oldString);
}
private:
std::string currentString;
std::string oldString;
};
void test_undoclass()
{
Undoable u;
u.setString("bar");
u.setString("foo");
std::cout << u.getString() << "\n";
u.undo();
std::cout << u.getString() << "\n";
u.undo();
std::cout << u.getString() << "\n";
u.setString("Getting some 🍺 tonight!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment