{"id":12113,"date":"2021-09-06T10:55:38","date_gmt":"2021-09-06T10:55:38","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=12113"},"modified":"2024-02-18T14:26:25","modified_gmt":"2024-02-18T14:26:25","slug":"deleter-stdshared_ptr-vs-stdunique_ptr","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=12113","title":{"rendered":"Deleter &#8211; std::shared_ptr vs. std::unique_ptr"},"content":{"rendered":"<p>For the std::unique_ptr the deleter is part of the type and the std::shared_ptr keeps it as part of its control block. Therefore in a Base-Derived-Class szenario, with a non virtual destructor in the base class, the std::unique_ptr will just delete the base class part and the std::shared_ptr also the derived class part.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &lt;iostream&gt;\r\n#include &lt;memory&gt;\r\n\r\nstruct Base\r\n{\r\n  ~Base()\r\n  {\r\n    std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;\r\n  }\r\n};\r\n\r\nstruct Derived : public Base\r\n{\r\n  ~Derived()\r\n  {\r\n    std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;\r\n  }\r\n};\r\n\r\nint main()\r\n{\r\n  {\r\n    std::cout &lt;&lt; &quot;unique_ptr&quot; &lt;&lt; std::endl;\r\n    std::unique_ptr&lt;Base&gt; uPtr(new Derived);\r\n  }\r\n  std::cout &lt;&lt; std::endl;\r\n  {\r\n    std::cout &lt;&lt; &quot;shared_ptr&quot; &lt;&lt; std::endl;\r\n    std::shared_ptr&lt;Base&gt; sPtr(new Derived);\r\n  }\r\n  return 0;\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n$ g++ main.cpp -std=c++11\r\n$ .\/a.out\r\nunique_ptr\r\n~Base\r\n\r\nshared_ptr\r\n~Derived\r\n~Base\r\n<\/pre>\n<p>Never the less please always make the destructor in the base class virtual.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the std::unique_ptr the deleter is part of the type and the std::shared_ptr keeps it as part of its control<\/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":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12113"}],"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=12113"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12113\/revisions"}],"predecessor-version":[{"id":16738,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12113\/revisions\/16738"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}