Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem here seems more a problem of design than a problem of swap.</p> <p>You (basically) use a pointer to a variable you did not assign yourself as an identifier of the kid... so of course as soon as the kids are moved around, it's going to break. (well it's slightly better than a pointer actually, but not by much)</p> <p>You have 2 solutions:</p> <ul> <li>the basic > instantiate them yourselves</li> <li>the more complicated > factory + id</li> </ul> <p>And of course you also have the problem that you tied up <code>Home</code> and its implementation... but I'll let you write a proper <code>Home</code> class by yourself. By the way, since the number of templates parameters of a STL container depend both on the container and on the actual implementation of the STL you are using, your solution is far less generic than using a real class ;)</p> <p><strong>Basic</strong>:</p> <pre><code>class kid { public: kid(kid* related = 0): m_related(related) {} // methods private: // other attributes kid* m_related; }; int main(int argc, char* argv[]) { Home home1, home2; kid* kid1 = home1.insert(new kid(home1, 1)); home1.insert(new kid(home1, 2, kid1)); kid* kid2 = home2.insert(new kid(home2, 1)); kid* kid3 = home2.insert(new kid(home2, 2)); home.insert(new kid(home2, 3, kid2)); home.insert(new kid(home2, 4, kid3)); swap(home1,home2); // just swapping the vectors, since it only moves pointers not the objects } </code></pre> <p>That's the easy way, but of course I still curse you because I have 2 kids with an id of 1...</p> <p><strong>Factory</strong></p> <pre><code> class KidFactory; class KidKey { friend class KidFactory; KidKey() {} }; class Kid { public: Kid(KidKey, Id id, Id cousinId); Id getId() const { return m_id; } const Home* getHome() const; void setHome(const Home&amp; home); private: const Home* m_home; Id m_id; Id m_cousinId; }; // Factory class KidFactory { public: Kid&amp; createKid(Id cousinId = -1) { m_kids.push_back(Kid(KidKey(), m_kids.size(), cousinId)); return m_kids.back(); } Kid&amp; getKid(Id id) { return m_kids.at(id); // notice that might generate out_of_range exception } private: std::vector&lt;Kid&gt; m_kids; }; class Home { public: void insert(Kid&amp; kid) { m_kids.push_back(kid.getId()); kid.setHome(*this); } }; int main(int argc, char* argv[]) { KidFactory factory; Home home1, home2; home1.insert(factory.createKid()); home1.insert(factory.createKid(home1.front().getId())); Kid&amp; kid1 = factory.createKid(); home2.insert(kid1); home2.insert(factory.createKid(kid1.getId())); // once again swapping vectors is fine } </code></pre> <p>Proper design leads to easy to write code... but some boilerplate one in C++ :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload