Please read the following code and tell me if it will cause problems in the future, and if so, how to avoid them.
class Note { int id; std::string text; public:
In short, I want the copy constructor to create an exact copy of the object, including its ID (database) field. On the other hand, when I assign, I just want to copy the data fields. But I have some problems, since usually a copy of ctor and operator = have the same semantics.
The id field is used only by Note and his friends. For all other clients, the assignment operator creates an exact copy. Use case. When I want to edit a note, I create a copy using a copy of ctor, edit it, and then call save in the Notebook class that controls Notes:
Note n(notebook.getNote(id)); n = editNote(n); // pass by const ref (for the case edit is canceled) notebook.saveNote(n);
On the other hand, I want to create a completely new note with the same contents as the existing note, I can do this:
Note n; n = notebook.getNote(id); n.setText("This is a copy"); notebook.addNote(n);
Is this approach believable? If not, indicate what are the possible negative consequences! Many thanks!
source share