0

I'm following C++ primer 5th edition and get confused by below "=" operator overloading with self assignment check. Below Message class has two member variables, a set of pointers to Folder objects and a message string.

My question is when I try to self assign a message object (example : m1 = m1) the set of pointer to Folder objects (std::set folders) reset to zero because of passing rhs parameter by reference and calling remove_from_Folders() function, but the author state that this implementation will handles self assignment correctly.

class Message
{
friend class Folder;
private:
    std::set<Folder*> folders;
    std::string content;
    void add_to_folders(const Message&);
    void remove_from_Folders();
public:
    explicit Message(const std::string str="");
    Message(const Message&);
    Message& operator=(const Message&);
    ~Message();
};

Message& Message::operator=(const Message &rhs)
{    
    // handle self-assignment by removing pointers before inserting them    
    remove_from_Folders();   // update existing Folders    
    contents = rhs.contents; // copy message contents from rhs    
    folders = rhs.folders;   // copy Folder pointers from rhs    
    add_to_Folders(rhs);     // add this Message to those Folders    
    return *this;
}

void Message::remove_from_Folders()
{    
    for (auto f : folders) // for each pointer in folders
        f->remMsg(this);   // remove this Message from that Folder
    folders.clear();       // no Folder points to this Message
}

C++ Primer 5th edition - Chapter - 13.4.
It would be really appreciated, if anyone help me on this.

Vencat
  • 1,272
  • 11
  • 36
  • That code can never work as it does indeed clear the `folder` data for both `this` and `rhs`. The proper self-assignment check would be something like `if (this == &rhs) return *this;` – Some programmer dude Sep 13 '19 at 08:07
  • 1
    I can understand that he is not checking **if (this == &rhd)** to avoid unnecessary performance degrade [Copy/swap](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom), but C++ primer is highly recommended book in C++ community that's why I'm confused. – Vencat Sep 13 '19 at 08:14
  • Just to clarify, it *is* the Lippman, Lajoie and Moo book you're reading, and not the Prata book C++ Primer Plus? – Some programmer dude Sep 13 '19 at 08:19
  • 1
    Yes, I'm reading stanley lippman C++ Primer in https://learning.oreilly.com – Vencat Sep 13 '19 at 08:28
  • Author probably wanted 2 version of `remove_from_Folders` (and `add_to_Folders`), one which update `folders` (public interface) and one not (for internal usage as for `operator=`). – Jarod42 Sep 13 '19 at 08:37
  • Searching around there seems to be quite a few small problems with the book but [the current errata](http://ptgmedia.pearsoncmg.com/images/9780321714114/errata/9780321714114_errata_10-31-12.html) is very old and doesn't list all of the problems (only the ones fixed for the second printing). This one I haven't seen listed in my (quick) search. – Some programmer dude Sep 13 '19 at 08:38
  • 1
    That looks like a bug in the book. All books have them. – molbdnilo Sep 13 '19 at 08:39
  • The book has other bugs as well. Look at [Error spotted in C++ Primer 5th Edition](https://stackoverflow.com/questions/66515465/error-spotted-in-c-primer-5th-edition-shared-ptrint/69445427#69445427) for example. – Jason Nov 03 '21 at 15:36

0 Answers0