<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[std::vector&amp;lt; boost::shared_ptr&amp;lt; Base&amp;gt; &amp;gt; initialisieren]]></title><description><![CDATA[<p>hi,</p>
<p>wie fügt man hier neue elemente hinzu?</p>
<pre><code class="language-cpp">std::vector&lt; boost::shared_ptr&lt; Base&gt; &gt; v;
v.push_back( new Child() );
</code></pre>
<p>oder anders?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/226438/std-vector-lt-boost-shared_ptr-lt-base-gt-gt-initialisieren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 00:00:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/226438.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Nov 2008 07:46:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::vector&amp;lt; boost::shared_ptr&amp;lt; Base&amp;gt; &amp;gt; initialisieren on Sat, 01 Nov 2008 07:46:06 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>wie fügt man hier neue elemente hinzu?</p>
<pre><code class="language-cpp">std::vector&lt; boost::shared_ptr&lt; Base&gt; &gt; v;
v.push_back( new Child() );
</code></pre>
<p>oder anders?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608403</guid><dc:creator><![CDATA[C-x C-c]]></dc:creator><pubDate>Sat, 01 Nov 2008 07:46:06 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector&amp;lt; boost::shared_ptr&amp;lt; Base&amp;gt; &amp;gt; initialisieren on Sat, 01 Nov 2008 08:01:46 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::vector&lt; boost::shared_ptr&lt;Base&gt; &gt; v;

boost::shared_ptr&lt;Base&gt; b;

v.push_back(b);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1608409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608409</guid><dc:creator><![CDATA[JustSomeCoder]]></dc:creator><pubDate>Sat, 01 Nov 2008 08:01:46 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector&amp;lt; boost::shared_ptr&amp;lt; Base&amp;gt; &amp;gt; initialisieren on Sat, 01 Nov 2008 08:08:26 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">v.push_back( boost::shared_ptr&lt;Base&gt;(new Child()) );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1608411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608411</guid><dc:creator><![CDATA[Lösungskugel]]></dc:creator><pubDate>Sat, 01 Nov 2008 08:08:26 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector&amp;lt; boost::shared_ptr&amp;lt; Base&amp;gt; &amp;gt; initialisieren on Sat, 01 Nov 2008 11:40:20 GMT]]></title><description><![CDATA[<p>Auf <a href="http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/shared_ptr.htm" rel="nofollow">boost.org</a> stehen besondere Hinweise zur Benutzung von <code>shared_ptr</code> :</p>
<blockquote>
<p><strong>Best Practices</strong><br />
A simple guideline that nearly eliminates the possibility of memory leaks is: always use a named smart pointer variable to hold the result of new. Every occurence of the new keyword in the code should have the form:</p>
<pre><code class="language-cpp">shared_ptr&lt;T&gt; p(new Y);
</code></pre>
<p>[...]<br />
Avoid using unnamed shared_ptr temporaries to save typing; to see why this is dangerous, consider this example:</p>
<pre><code class="language-cpp">void f(shared_ptr&lt;int&gt;, int);
int g();

void ok()
{
    shared_ptr&lt;int&gt; p(new int(2));
    f(p, g());
}

void bad()
{
    f(shared_ptr&lt;int&gt;(new int(2)), g());
}
</code></pre>
<p>The function ok follows the guideline to the letter, whereas bad constructs the temporary shared_ptr in place, admitting the possibility of a memory leak. Since function arguments are evaluated in unspecified order, it is possible for new int(2) to be evaluated first, g() second, and we may never get to the shared_ptr constructor if g throws an exception. See Herb Sutter's treatment (also here) of the issue for more information.</p>
</blockquote>
<p>Man verliert in diesem Fall die Exceptionsicherheit.</p>
<p>Ausserdem gibts für Container mit Zeiger eine eigene Boost-Bibliothek, nämlich <em>Boost.Pointer Container</em>. Damit ist man flexibler und kann sich beim Iterieren und Zugreifen eine Indirektion sparen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608469</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608469</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 01 Nov 2008 11:40:20 GMT</pubDate></item></channel></rss>