{"id":39937,"date":"2023-12-11T14:49:16","date_gmt":"2023-12-11T06:49:16","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=39937"},"modified":"2023-12-11T14:49:16","modified_gmt":"2023-12-11T06:49:16","slug":"%e8%af%b7%e4%bd%a0%e6%9d%a5%e8%af%b4%e4%b8%80%e4%b8%8b%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88shared_ptr%e7%9a%84%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e8%af%b7%e4%bd%a0%e6%9d%a5%e8%af%b4%e4%b8%80%e4%b8%8b%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88shared_ptr%e7%9a%84%e5%ae%9e%e7%8e%b0%ef%bc%9f\/","title":{"rendered":"\u8bf7\u4f60\u6765\u8bf4\u4e00\u4e0b\u667a\u80fd\u6307\u9488shared_ptr\u7684\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u6838\u5fc3\u8981\u7406\u89e3\u5f15\u7528\u8ba1\u6570\uff0c\u4ec0\u4e48\u65f6\u5019\u9500\u6bc1\u5e95\u5c42\u6307\u9488\uff0c\u8fd8\u6709\u8d4b\u503c\uff0c\u62f7\u8d1d\u6784\u9020\u65f6\u5019\u7684\u5f15\u7528\u8ba1\u6570\u7684\u53d8\u5316\uff0c\u6790\u6784\u7684\u65f6\u5019\u8981\u5224\u65ad\u5e95\u5c42\u6307\u9488\u7684\u5f15\u7528\u8ba1\u6570\u4e3a0\u4e86\u624d\u80fd\u771f\u6b63\u91ca\u653e\u5e95\u5c42\u6307\u9488\u7684\u5185\u5b58**<\/p>\n<h3><strong>template **<\/strong>class SmartPtr<strong><\/strong>{<strong><\/strong>private:<strong><\/strong>T *ptr;   \/\/\u5e95\u5c42\u771f\u5b9e\u7684\u6307\u9488<strong><\/strong>int *use_count;\/\/\u4fdd\u5b58\u5f53\u524d\u5bf9\u8c61\u88ab\u591a\u5c11\u6307\u9488\u5f15\u7528\u8ba1\u6570<strong><\/strong>public:<strong><\/strong>SmartPtr(T *p); \/\/SmartPtrp(new int(2));<strong><\/strong>SmartPtr(const SmartPtr&amp;orig);\/\/SmartPtrq(p);<strong><\/strong>SmartPtr&amp;operator=(const SmartPtr &amp;rhs);\/\/q=p<strong><\/strong>~SmartPtr();<strong><\/strong>T operator*();  \/\/\u4e3a\u4e86\u80fd\u628a\u667a\u80fd\u6307\u9488\u5f53\u6210\u666e\u901a\u6307\u9488\u64cd\u4f5c\u5b9a\u4e49\u89e3\u5f15\u7528\u64cd\u4f5c<strong><\/strong>T*operator-&gt;();  \/\/\u5b9a\u4e49\u53d6\u6210\u5458\u64cd\u4f5c<strong><\/strong>T* operator+(int i);\/\/\u5b9a\u4e49\u6307\u9488\u52a0\u4e00\u4e2a\u5e38\u6570<strong><\/strong>int operator-(SmartPtr&amp;t1,SmartPtr&amp;t2);\/\/\u5b9a\u4e49\u4e24\u4e2a\u6307\u9488\u76f8\u51cf<strong>`<\/strong>void getcount()** <strong>{<\/strong> <strong>return *use_count<\/strong> <strong>}<\/strong> <strong>};<\/strong> <strong>template <\/strong> <strong>int SmartPtr::operator-(SmartPtr &amp;t1, SmartPtr &amp;t2)<\/strong> <strong>{<\/strong> <strong>return t1.ptr-t2.ptr;<\/strong> <strong>}<\/strong> <strong>template <\/strong> <strong>SmartPtr::SmartPtr(T *p)<\/strong> <strong>{<\/strong> <strong>ptr=p;<\/strong> <strong>try<\/strong> <strong>{<\/strong> <strong>use_count=new int(1);<\/strong> <strong>}catch (&#8230;)<\/strong> <strong>{<\/strong><code>**delete ptr<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span>   \/\/\u7533\u8bf7\u5931\u8d25\u91ca\u653e\u771f\u5b9e\u6307\u9488\u548c\u5f15\u7528\u8ba1\u6570\u7684\u5185\u5b58**<\/code><strong>ptr= nullptr;<\/strong> <strong>delete use_count;<\/strong> <strong>use_count= nullptr;<\/strong> <strong>}<\/strong> <strong>}<\/strong> <strong>template <\/strong><code>**SmartPtr::SmartPtr(const SmartPtr &amp;orig) \/\/\u590d\u5236\u6784\u9020\u51fd\u6570****{****use_count=orig.use_count<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span>\/\/\u5f15\u7528\u8ba1\u6570\u4fdd\u5b58\u5728\u4e00\u5757\u5185\u5b58\uff0c\u6240\u6709\u7684SmarPtr\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u90fd\u6307\u5411\u8fd9\u91cc****this<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">-<\/span>&gt;ptr=orig.ptr<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span>****++(\\*use_count)<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span>\/\/\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u52a01**<\/code><strong>}<\/strong> <strong>template <\/strong> <strong>SmartPtr&amp; SmartPtr::operator=(const SmartPtr &amp;rhs)<\/strong> <strong>{<\/strong><code>**\/\/\u91cd\u8f7d=\u8fd0\u7b97\u7b26\uff0c\u4f8b\u5982SmartPtrp,q<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span> p=q<span style=\"\"font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif\"\">;<\/span>\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u9996\u5148\u7ed9q\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u52a01\uff0c\u56e0\u4e3ap\u91cd\u65b0\u6307\u5411\u4e86q\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5p\u9700\u8981\u5148\u7ed9\u539f\u6765\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u51cf1\uff0c\u5982\u679c\u51cf\u4e00\u540e\u4e3a0\uff0c\u5148\u91ca\u653e\u6389p\u539f\u6765\u6307\u5411\u7684\u5185\u5b58\uff0c\u7136\u540e\u8bb2q\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u52a01\u540e\u8d4b\u503c\u7ed9p**<\/code><strong>++*(rhs.use_count);<\/strong> <strong>if((&#8211;*(use_count))==0)<\/strong> <strong>{<\/strong> <strong>delete ptr;<\/strong> <strong>ptr= nullptr;<\/strong> <strong>delete use_count;<\/strong> <strong>use_count= nullptr;<\/strong> <strong>}<\/strong> <strong>ptr=rhs.ptr;<\/strong> <strong>*use_count=*(rhs.use_count);<\/strong> <strong>return *this;<\/strong> <strong>}<\/strong> <strong>template <\/strong> <strong>SmartPtr::~SmartPtr()<\/strong> <strong>{<\/strong> <strong>getcount();<\/strong>`<strong>if(&#8211;(*use_count)==0)  \/\/SmartPtr\u7684\u5bf9\u8c61\u4f1a\u5728\u5176\u751f\u547d\u5468\u671f\u7ed3\u675f\u7684\u65f6\u5019\u8c03\u7528\u5176\u6790\u6784\u51fd\u6570\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u68c0\u6d4b\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u662f\u4e0d\u662f\u53ea\u6709\u6b63\u5728\u7ed3\u675f\u751f\u547d\u5468\u671f\u7684\u8fd9\u4e2aSmartPtr\u5f15\u7528\uff0c\u5982\u679c\u662f\uff0c\u5c31\u91ca\u653e\u6389\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5c31\u8fd8\u6709\u5176\u4ed6\u7684SmartPtr\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\uff0c\u5c31\u7b49\u5f85\u5176\u4ed6\u7684SmartPtr\u5bf9\u8c61\u5728\u5176\u751f\u547d\u5468\u671f\u7ed3\u675f\u7684\u65f6\u5019\u8c03\u7528\u6790\u6784\u51fd\u6570\u91ca\u653e\u6389<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre><code>{getcount();delete` `ptr;ptr= nullptr;delete` `use_count;use_count=nullptr;}}template` `&lt;``typename` `T&gt;T SmartPtr::operator*(){return` `*ptr;}template` `&lt;``typename` `T&gt;T*  SmartPtr::operator-&gt;(){return` `ptr;}template` `&lt;``typename` `T&gt;T* SmartPtr::operator+(``int` `i){T *temp=ptr+i;return` `temp;}}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre><code>            \"```\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u6838\u5fc3\u8981\u7406\u89e3\u5f15\u7528\u8ba1\u6570\uff0c\u4ec0\u4e48\u65f6\u5019\u9500\u6bc1\u5e95\u5c42\u6307\u9488\uff0c\u8fd8\u6709\u8d4b\u503c\uff0c\u62f7\u8d1d\u6784\u9020\u65f6\u5019\u7684\u5f15\u7528\u8ba1\u6570\u7684\u53d8 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-39937","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/39937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=39937"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/39937\/revisions"}],"predecessor-version":[{"id":39938,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/39937\/revisions\/39938"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=39937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=39937"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=39937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}