<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>cpp &amp;mdash; Little Step</title>
    <link>https://rex.writeas.com/tag:cpp</link>
    <description></description>
    <pubDate>Sat, 09 May 2026 23:23:57 +0000</pubDate>
    <item>
      <title>Use std::enable\shared\from\_this for this in smart pointer mode</title>
      <link>https://rex.writeas.com/use-std-enablesharedfrom_this-for-this-in-smart-point-mode?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[As we all know that there is a this pointer for all objects, but if we want to use smart pointer, how to we use.&#xA;&#xA;It turns out that it&#39;s not that easy by creating a share\ptr from this pointer, since we may have many shared\ptrs points to the same object with without knowing each other.&#xA;&#xA;Taking this code as an example, if we create a share\ptr by dangerous function, we will have two share\ptrs pointing to the same object. Both sp1 and sp2 point to same object, and when the two exits it leads problem.&#xA;&#xA;struct S&#xA;{&#xA;  sharedptrS dangerous() {&#xA;    return sharedptrS(this);  // don&#39;t do this!&#xA;  }&#xA;};&#xA;&#xA;int main() {&#xA;  sharedptrS sp1(new S);&#xA;  sharedptrS sp2 = sp-  dangerous();&#xA;  return 0;&#xA;}&#xA;&#xA;How to fix this problem, use the std::enable\shared\from\this help class defined inmemory, it is introduced in C++11.&#xA;&#xA;struct S: enablesharedfromthisS {&#xA;  sharedptrS dangerous() {&#xA;    return sharedfromthis();&#xA;  }&#xA;};&#xA;&#xA;int main() {&#xA;  sharedptrS sp1(new S);&#xA;  sharedptrS sp2 = sp-  dangerous();  // not dangerous&#xA;&#xA;  return 0;&#xA;}&#xA;&#xA;#cpp #share\ptr]]&gt;</description>
      <content:encoded><![CDATA[<p>As we all know that there is a <strong>this</strong> pointer for all objects, but if we want to use smart pointer, how to we use.</p>

<p>It turns out that it&#39;s not that easy by creating a share_ptr from this pointer, since we may have many shared_ptrs points to the same object with without knowing each other.</p>

<p>Taking this code as an example, if we create a share_ptr by dangerous function, we will have two share_ptrs pointing to the same object. Both sp1 and sp2 point to same object, and when the two exits it leads problem.</p>

<pre><code class="language-cpp">struct S
{
  shared_ptr&lt;S&gt; dangerous() {
    return shared_ptr&lt;S&gt;(this);  // don&#39;t do this!
  }
};

int main() {
  shared_ptr&lt;S&gt; sp1(new S);
  shared_ptr&lt;S&gt; sp2 = sp-&gt;dangerous();
  return 0;
}
</code></pre>

<p>How to fix this problem, use the <em>std::enable_shared_from_this</em> help class defined in, it is introduced in C++11.</p>

<pre><code class="language-cpp">struct S: enable_shared_from_this&lt;S&gt; {
  shared_ptr&lt;S&gt; dangerous() {
    return shared_from_this();
  }
};

int main() {
  shared_ptr&lt;S&gt; sp1(new S);
  shared_ptr&lt;S&gt; sp2 = sp-&gt;dangerous();  // not dangerous

  return 0;
}
</code></pre>

<p><a href="https://rex.writeas.com/tag:cpp" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">cpp</span></a> <a href="https://rex.writeas.com/tag:share" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">share</span></a>_ptr</p>
]]></content:encoded>
      <guid>https://rex.writeas.com/use-std-enablesharedfrom_this-for-this-in-smart-point-mode</guid>
      <pubDate>Wed, 24 Aug 2022 08:09:16 +0000</pubDate>
    </item>
    <item>
      <title>C++ alternative operators</title>
      <link>https://rex.writeas.com/c-alternative-operators?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[As a C++ programmer for many years, I never noticed the other style of logic operators in C++ until today.&#xA;&#xA;When I review part of code witten by one of my colleague which is a mainly a Java programmer, and find he uses and other than &amp;&amp; in if statement.&#xA;&#xA;And I check cppreference, and find the C++ operator alternative.&#xA;&#xA;| Primary      | Alternative |&#xA;|------------ |----------- |&#xA;| &amp;&amp;           | and         |&#xA;| &amp;=           | and\eq     |&#xA;| &amp;            | bitand      |&#xA;| &amp;vert;       | bitor       |&#xA;| ~            | compl       |&#xA;| !            | not         |&#xA;| !=           | not\eq     |&#xA;| &amp;vert;&amp;vert; | or          |&#xA;| &amp;vert;=      | or\eq      |&#xA;| ^            | xor         |&#xA;| ^=           | xor\_eq     |&#xA;| {            | &lt;%          |&#xA;| }            | %  |&#xA;| [            | &lt;:          |&#xA;| ]            | :  |&#xA;| #            | %:          |&#xA;| ##           | %:%:        |&#xA;&#xA;The reason for alternative operators is that the source code maybe not in ASCII set, and don&#39;t have the proper operators.&#xA;&#xA;\#cpp #programming #operator]]&gt;</description>
      <content:encoded><![CDATA[<p>As a C++ programmer for many years, I never noticed the other style of logic operators in C++ until today.</p>

<p>When I review part of code witten by one of my colleague which is a mainly a Java programmer, and find he uses <strong>and</strong> other than <strong>&amp;&amp;</strong> in if statement.</p>

<p>And I check cppreference, and find the <a href="https://en.cppreference.com/w/cpp/language/operator_alternative" rel="nofollow">C++ operator alternative</a>.</p>

<table>
<thead>
<tr>
<th>Primary</th>
<th>Alternative</th>
</tr>
</thead>

<tbody>
<tr>
<td>&amp;&amp;</td>
<td>and</td>
</tr>

<tr>
<td>&amp;=</td>
<td>and_eq</td>
</tr>

<tr>
<td>&amp;</td>
<td>bitand</td>
</tr>

<tr>
<td>|</td>
<td>bitor</td>
</tr>

<tr>
<td>~</td>
<td>compl</td>
</tr>

<tr>
<td>!</td>
<td>not</td>
</tr>

<tr>
<td>!=</td>
<td>not_eq</td>
</tr>

<tr>
<td>||</td>
<td>or</td>
</tr>

<tr>
<td>|=</td>
<td>or_eq</td>
</tr>

<tr>
<td>^</td>
<td>xor</td>
</tr>

<tr>
<td>^=</td>
<td>xor_eq</td>
</tr>

<tr>
<td>{</td>
<td>&lt;%</td>
</tr>

<tr>
<td>}</td>
<td>%&gt;</td>
</tr>

<tr>
<td>[</td>
<td>&lt;:</td>
</tr>

<tr>
<td>]</td>
<td>:&gt;</td>
</tr>

<tr>
<td>#</td>
<td>%:</td>
</tr>

<tr>
<td>##</td>
<td>%:%:</td>
</tr>
</tbody>
</table>

<p>The reason for alternative operators is that the source code maybe not in ASCII set, and don&#39;t have the proper operators.</p>

<p>#cpp <a href="https://rex.writeas.com/tag:programming" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">programming</span></a> <a href="https://rex.writeas.com/tag:operator" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">operator</span></a></p>
]]></content:encoded>
      <guid>https://rex.writeas.com/c-alternative-operators</guid>
      <pubDate>Fri, 10 Jan 2020 02:24:48 +0000</pubDate>
    </item>
  </channel>
</rss>