title: Save or discard user changes to object
body: <p>I have an application where the user can change data on screen in a form (represented as objects in c++ code), and when the user wants to close the window I want to ask if changes should be saved or discarded. </p>
  
  <p>If the user wants to save, the edited object should be persisted to the database and the in-memory object should of course also be up to date.</p>
  
  <p>If the user wants to discard the changes, I want the in-memory object to be restored to its previous state.</p>
  
  <p>I am not sure how to implement this as efficiently as possible, especially the discarding of changes. These are some options I am considering:</p>
  
  <ol>
  <li><p>I create a copy of the in-memory object somewhere in the form logic and change it, and if the user wants to save it I replace the previous in-memory object with the changed one.</p></li>
  <li><p>Same as above but the object somehow stores a copy of itself instead of putting this in the form logic.</p></li>
  <li><p>I keep track of which form fields have changed and only update the relevant object members if the user wants to save (avoiding a potentially large copy operation)?</p></li>
  </ol>
  
  <p>This seems like such a common thing to do in applications, so maybe there is a standard way to do this, or a design pattern?</p>