<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/2030/1/7/_/styles/combined.css?spaceKey=TOP&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=9568664">C++ Best Practices</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://wiki.asterisk.org/wiki/display/~dlee">David M. Lee</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>This page is a high level list of general C++ best practices, do's and don'ts, etc.</td></tr></table></div>

<div>
<ul>
    <li><a href='#C%2B%2BBestPractices-Don%27tuse%7B%7Bstd%3A%3Aautoptr%7D%7D.Use%7B%7Bboost%3A%3Asharedptr%7D%7Dinstead.'>Don't use <tt>std::auto_ptr</tt>. Use <tt>boost::shared_ptr</tt> instead.</a></li>
    <li><a href='#C%2B%2BBestPractices-Watchoutforcyclicreferenceswhenusingreferencecountingsmartpointers.'>Watch out for cyclic references when using reference counting smart pointers.</a></li>
    <li><a href='#C%2B%2BBestPractices-Passsmartpointersas%7B%7Bconst%26%7D%7Dinsteadofbyvalue'>Pass smart pointers as <tt>const &amp;</tt> instead of by value</a></li>
</ul></div>

<h3><a name="C%2B%2BBestPractices-Don%27tuse%7B%7Bstd%3A%3Aautoptr%7D%7D.Use%7B%7Bboost%3A%3Asharedptr%7D%7Dinstead."></a>Don't use <tt>std::auto_ptr</tt>.  Use <tt>boost::shared_ptr</tt> instead.</h3>
<p>There are some nuances to <tt>std::auto_ptr</tt> that can be problematic if you're not careful.  It's even been deprecated in C++0x.  As a general rule, we just avoid <tt>std::auto_ptr</tt> to avoid the problems.</p>

<p>See <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=400" class="external-link" rel="nofollow">this article</a> for more details.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: cpp; gutter: false"><![CDATA[
// use shared_ptr instead of auto_ptr
boost::shared_ptr&lt;Foo&gt; buildFoo()
{
  return boost::shared_ptr&lt;Foo&gt;(new Foo);
}
]]></script>
</div></div>

<h3><a name="C%2B%2BBestPractices-Watchoutforcyclicreferenceswhenusingreferencecountingsmartpointers."></a>Watch out for cyclic references when using reference counting smart pointers.</h3>
<p>When using reference counting pointers (<tt>boost::shared_ptr</tt>, Ice <tt>Ptr</tt> types), avoid cyclic references of objects.  These could lead to memory leaks, which (ironically) defeats the purpose of the smart pointers in the first place.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: cpp; gutter: false"><![CDATA[
// avoid cyclic references with reference counting pointers

//
// WARNING!!!  Bad code follows
//
class Bar;

class Foo
{
public:
    void setBar(const boost::shared_ptr&lt;Bar&gt;&amp; bar) { mBar = bar; }
private:
    boost::shared_ptr&lt;Bar&gt; mBar;
};

class Bar
{
public:
    void setFoo(const boost::shared_ptr&lt;Foo&gt;&amp; foo) { mFoo = foo; }
private:
    boost::shared_ptr&lt;Foo&gt; mFoo;  // uh oh; cyclic reference could mean trouble!
};

void leak()
{
    boost::shared_ptr&lt;Foo&gt; f(new Foo());
    boost::shared_ptr&lt;Bar&gt; b(new Bar());
    f-&gt;setBar(b);
    b-&gt;setFoo(f);
    // YIKES!  f and b have just leaked.
}
]]></script>
</div></div>

<h3><a name="C%2B%2BBestPractices-Passsmartpointersas%7B%7Bconst%26%7D%7Dinsteadofbyvalue"></a>Pass smart pointers as <tt>const &amp;</tt> instead of by value</h3>
<p>When passing smart pointers (<tt>boost_shared_ptr</tt>, Ice <tt>Ptr</tt> types, Ice <tt>Prx</tt> types) as function parameters, pass them as <tt>const &amp;</tt> instead of pass-by value.  This saves on some needless intermediate copies, since the caller should hold their reference to the object for the duration of the call.</p>

<p>But <b>do not</b> store members or local variables as <tt>const &amp;</tt>.  This would fail to increment reference counts, so an object may get deleted before its time.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: cpp; gutter: false"><![CDATA[
// pass smart pointers by const reference

// fizzgop a Foo and Bar together
void fizzgop(const boost::shared_ptr&lt;Foo&gt;&amp; foo, const boost::shared_ptr&lt;Bar&gt;&amp; bar)
{
    // pretend there's fizzgoping going on here

    // WARNING!  this is wrong.
    const boost::shared_ptr&lt;Bang&gt;&amp; bang = findAppropriateBang(foo, bar);
}
]]></script>
</div></div>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="https://wiki.asterisk.org/wiki/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
       </div>
       <a href="https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=9568664">View Online</a>
              |
       <a href="https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=9568664&showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>