{"id":1658,"date":"2018-09-07T13:53:21","date_gmt":"2018-09-07T13:53:21","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=1658"},"modified":"2024-02-16T11:12:00","modified_gmt":"2024-02-16T11:12:00","slug":"destruction-order-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=1658","title":{"rendered":"Destruction order (C++)"},"content":{"rendered":"<p>The variables\/fields getting cleanup in the reverse order they have been declared (not initialized).<\/p>\n<hr>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n\r\nclass Text {\r\npublic:\r\n  Text(std::string text) : m_text(text) {}\r\n  ~Text() { std::cout &lt;&lt; &quot;Destruct Text &#039;&quot; + toStr() + &quot;&#039;&quot; &lt;&lt; std::endl; }\r\n  std::string toStr() { return m_text; }\r\nprivate:\r\n  std::string m_text;\r\n};\r\n\r\nclass Number {\r\npublic:\r\n  Number(unsigned number) : m_number(number) {}\r\n  ~Number() { std::cout &lt;&lt; &quot;Destruct Number &#039;&quot; + toStr() + &quot;&#039;&quot; &lt;&lt; std::endl; }\r\n  std::string toStr() { return std::to_string(m_number); }\r\nprivate:\r\n  unsigned m_number;\r\n};\r\n\r\nclass Track {\r\npublic:\r\n  Track(Text title, Number duration)\r\n    : m_title(title), m_duration(duration) {}\r\n  virtual ~Track() {\r\n    std::cout &lt;&lt; &quot;Destruct Track &#039;&quot;\r\n              + m_title.toStr() + &quot;&#039;, &#039;&quot; + m_duration.toStr() + &quot;&#039;&quot;\r\n              &lt;&lt; std::endl;\r\n  }\r\nprotected:\r\n  Text m_title;\r\n  Number m_duration;\r\n};\r\n\r\nclass CloudTrack : public Track {\r\npublic:\r\n  CloudTrack(Text title, Number duration, Text service, Text uuid)\r\n    : Track(title, duration), m_service(service), m_uuid(uuid) {}\r\n  ~CloudTrack() {\r\n    std::cout &lt;&lt; &quot;Destruct CloudTrack &#039;&quot;\r\n              + m_title.toStr() + &quot;&#039;, &#039;&quot; + m_duration.toStr() + &quot;&#039;, &#039;&quot;\r\n              + m_service.toStr() + &quot;&#039;, &#039;&quot; + m_uuid.toStr() + &quot;&#039;&quot;\r\n              &lt;&lt; std::endl;\r\n  }\r\nprivate:\r\n  Text m_service;\r\n  Text m_uuid;\r\n};\r\n\r\nclass TrackVector {\r\npublic:\r\n  ~TrackVector() { std::cout &lt;&lt; &quot;Destruct TrackVector&quot; &lt;&lt; std::endl; }\r\n  void push_back(std::unique_ptr&lt;Track&gt; track) {\r\n    m_vector.push_back(std::move(track));\r\n  }\r\nprivate:\r\n  std::vector&lt;std::unique_ptr&lt;Track&gt;&gt; m_vector;\r\n};\r\n\r\nclass Album {\r\npublic:\r\n  Album(Text title, Text artist) : m_title(title), m_artist(artist) {}\r\n  ~Album() { std::cout &lt;&lt; &quot;Destruct Album &#039;&quot;\r\n                       + m_title.toStr() + &quot;&#039;, &#039;&quot; + m_artist.toStr() + &quot;&#039;&quot;\r\n                       &lt;&lt; std::endl; }\r\n  void addTrack(std::unique_ptr&lt;Track&gt; track) {\r\n    m_tracks.push_back(std::move(track));\r\n  }\r\nprivate:\r\n  Text m_title;\r\n  Text m_artist;\r\n  TrackVector m_tracks;\r\n};\r\n\r\nint main()\r\n{\r\n  Album album(Text(&quot;Trash&quot;), Text(&quot;Alice Cooper&quot;));\r\n\r\n  album.addTrack(std::make_unique&lt;CloudTrack&gt;(Text(&quot;Poison&quot;), Number(270),\r\n    Text(&quot;Spotify&quot;), Text(&quot;5XcZRgJv3zMhTqCyESjQrF?si=d915382bc9954b34&quot;)));\r\n  album.addTrack(std::make_unique&lt;CloudTrack&gt;(Text(&quot;Trash&quot;), Number(243),\r\n    Text(&quot;Spotify&quot;), Text(&quot;69FKPvGvSKIqHxGLDYApYH?si=105d56c3b64b41bb&quot;)));\r\n\r\n  std::cout &lt;&lt; &quot;Objects are created&quot; &lt;&lt; std::endl;\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<pre>\r\n...\r\nObjects are created\r\n# Cleanup fields of album (Last declared variable in main)\r\nDestruct Album 'Trash', 'Alice Cooper'\r\n\r\n## Cleanup album.m_tracks (Last declared field of Album)\r\nDestruct TrackVector\r\n\r\n## Cleanup album.m_track[0]\r\n### Cleanup CloudTrack fields\r\nDestruct CloudTrack 'Poison', '270', 'Spotify', '5XcZRgJv3zMh #...\r\n#### Cleanup album.tracks[0].m_service (Last declared field of CloudTrack)\r\nDestruct Text '5XcZRgJv3zMhTqCyESjQrF?si=d915382bc9954b34'\r\n#### Cleanup album.tracks[0].m_uuid (First declared field of CloudTrack)\r\nDestruct Text 'Spotify'\r\n### Cleanup Track fields\r\nDestruct Track 'Poison', '270'\r\n#### Cleanup album.tracks[0].m_duration (Last declared field of Track)\r\nDestruct Number '270'\r\n#### Cleanup album.tracks[0].m_title (First declared field of Track)\r\nDestruct Text 'Poison'\r\n\r\n## Cleanup album.m_track[1]\r\n### Cleanup CloudTrack fields\r\nDestruct CloudTrack 'Trash', '243', 'Spotify', '69FKPvGvSKIq #...\r\n#### Cleanup album.tracks[1].m_service (Last declared field of CloudTrack)\r\nDestruct Text '69FKPvGvSKIqHxGLDYApYH?si=105d56c3b64b41bb'\r\n#### Cleanup album.tracks[1].m_uuid (First declared field of CloudTrack)\r\nDestruct Text 'Spotify'\r\n### Cleanup Track fields\r\nDestruct Track 'Trash', '243'\r\n#### Cleanup album.tracks[1].m_duration (Last declared field of Track)\r\nDestruct Number '243'\r\n#### Cleanup album.tracks[1].m_title (First declared field of Track)\r\nDestruct Text 'Trash'\r\n\r\n## Cleanup album.m_artist (Midway declared field of Album)\r\nDestruct Text 'Alice Cooper'\r\n\r\n## Cleanup album.m_title (First declared field of Album)\r\nDestruct Text 'Trash'\r\n<\/pre>\n<hr>\n<p><u>Disclaimer<\/u>: Keep in mind that if you have explicitly requested space on the heap you have to free\/delete it by your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The variables\/fields getting cleanup in the reverse order they have been declared (not initialized). &#8230; Objects are created # Cleanup<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1658"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1658"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":16781,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions\/16781"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}