<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>smart pointers Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/smart-pointers/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/smart-pointers/</link>
	<description></description>
	<lastBuildDate>Thu, 11 Jun 2020 00:59:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>smart pointers Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/smart-pointers/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>C++ 11 Smart Pointers 智能指针</title>
		<link>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/</link>
					<comments>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 11 Jun 2020 00:55:32 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[smart pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6896</guid>

					<description><![CDATA[<p>Example code [crayon-663a0e8bd9af3553099115/] Output [crayon-663a0e8bd9af9428833091/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Smart Pointers 智能指针 - C++ Weekly EP3" width="500" height="375" src="https://www.youtube.com/embed/KQt3IjGdqL8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Example code</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;memory&gt;

class Entity {
public:
  Entity() { puts(&quot;Entity created!&quot;); }
  ~Entity() { puts(&quot;Entity destroyed!&quot;); }
};

void ex1() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex1&quot;);
  {
    puts(&quot;Entering ex1::scope1&quot;);
    auto e1 = std::make_unique&lt;Entity&gt;();    
    puts(&quot;Leaving ex1::scope1&quot;);
  }
  puts(&quot;Leaving ex1&quot;);
}

void foo(std::unique_ptr&lt;Entity&gt;) {
  puts(&quot;Entering foo&quot;);
  puts(&quot;Leaving foo&quot;);
}

void ex2() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex2&quot;);
  auto e1 = std::make_unique&lt;Entity&gt;();  
  foo(std::move(e1));
  // e1 was destoried.
  puts(&quot;Leaving ex2&quot;);
}

void ex3() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex3&quot;);
  auto e1 = std::make_shared&lt;Entity&gt;();
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  {
    puts(&quot;Entering ex3::scope1&quot;);
    auto e2 = e1; // use_count ++
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    auto e3 = std::move(e2); // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    puts(&quot;Leaving ex3::scope1&quot;);
  }
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  puts(&quot;Leaving ex3&quot;);
}

void observe(std::weak_ptr&lt;Entity&gt; ew) {
  if (std::shared_ptr&lt;Entity&gt; spt = ew.lock()) {
    std::cout &lt;&lt; spt.use_count() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;entity still alive!&quot; &lt;&lt; std::endl;
  } else {
    std::cout &lt;&lt; &quot;entity was expired :(&quot; &lt;&lt; std::endl;
  }
}

void ex4() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex4&quot;);
  std::weak_ptr&lt;Entity&gt; ew;  
  {
    puts(&quot;Entering ex4::scope1&quot;);
    auto e1 = std::make_shared&lt;Entity&gt;();
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    ew = e1; // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    observe(ew);
    puts(&quot;Leaving ex4::scope1&quot;);
  }
  observe(ew);
  puts(&quot;Leaving ex4&quot;);
}

int main(int argc, char** argv) {
  ex1();
  ex2();
  ex3();
  ex4();
  return 0;
}</pre>
</div></div>



<h2>Output</h2>



<pre class="crayon-plain-tag">--------
Entering ex1
Entering ex1::scope1
Entity created!
Leaving ex1::scope1
Entity destroyed!
Leaving ex1
--------
Entering ex2
Entity created!
Entering foo
Leaving foo
Entity destroyed!
Leaving ex2
--------
Entering ex3
Entity created!
1
Entering ex3::scope1
2
2
Leaving ex3::scope1
1
Leaving ex3
Entity destroyed!
--------
Entering ex4
Entering ex4::scope1
Entity created!
1
1
2
entity still alive!
Leaving ex4::scope1
Entity destroyed!
entity was expired :(
Leaving ex4</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
