<?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[ref, ptr oder shared_ptr]]></title><description><![CDATA[<ol>
<li>Klasse A benötigt Zugriff auf eine Instanz von B.</li>
<li>B wird niemals (konzeptionell unmöglich) vor A zerstört.</li>
<li>A besitzt für die komplette Lebenszeit immer dasselbe B.</li>
</ol>
<p>Sollte A dann B als eine Referenz, ein Zeiger oder ein shared_ptr besitzen?</p>
<p>? ist ein Platzhalter</p>
<pre><code>class B {

};

class A {
private:
    ? member_;
public:
    A(? member);
    A() = delete;
    ~A() = default;
    A&amp; operator=(A const&amp;) = delete; // EDIT
    A(A const&amp;) = delete; // EDIT
};

// ? = B*
// ? = B&amp;
// ? = std::shared_ptr &lt;B&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/320817/ref-ptr-oder-shared_ptr</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 16:20:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/320817.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 13 Oct 2013 18:24:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Sun, 13 Oct 2013 18:35:59 GMT]]></title><description><![CDATA[<ol>
<li>Klasse A benötigt Zugriff auf eine Instanz von B.</li>
<li>B wird niemals (konzeptionell unmöglich) vor A zerstört.</li>
<li>A besitzt für die komplette Lebenszeit immer dasselbe B.</li>
</ol>
<p>Sollte A dann B als eine Referenz, ein Zeiger oder ein shared_ptr besitzen?</p>
<p>? ist ein Platzhalter</p>
<pre><code>class B {

};

class A {
private:
    ? member_;
public:
    A(? member);
    A() = delete;
    ~A() = default;
    A&amp; operator=(A const&amp;) = delete; // EDIT
    A(A const&amp;) = delete; // EDIT
};

// ? = B*
// ? = B&amp;
// ? = std::shared_ptr &lt;B&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2360016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360016</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Sun, 13 Oct 2013 18:35:59 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Sun, 13 Oct 2013 18:32:12 GMT]]></title><description><![CDATA[<blockquote>
<ol>
<li>Klasse A benötigt Zugriff auf eine Instanz von B.</li>
</ol>
</blockquote>
<p>A braucht demnach irgendeine Art von Verweis auf eine Instanz von B.</p>
<blockquote>
<ol start="2">
<li>B wird niemals (konzeptionell unmöglich) vor A zerstört.</li>
</ol>
</blockquote>
<p>A wird also immer vor B zerstört. Das heißt, A darf die B-Instanz nicht &quot;besitzen&quot;, da es diese nie zerstören wird.</p>
<blockquote>
<ol start="3">
<li>A besitzt für die komplette Lebenszeit immer dasselbe B.</li>
</ol>
</blockquote>
<p>Der Verweis verändert sich also nicht.</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /> <s>Referenz</s> konstanter Zeiger.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360019</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360019</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 13 Oct 2013 18:32:12 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Sun, 13 Oct 2013 18:34:18 GMT]]></title><description><![CDATA[<p>Tim06TR schrieb:</p>
<blockquote>
<ol start="3">
<li>A besitzt für die komplette Lebenszeit immer dasselbe B.</li>
</ol>
</blockquote>
<p>Bist du sicher? Wäre es nicht sinnvoll, ein swap/operator= anzubieten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360021</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360021</guid><dc:creator><![CDATA[reff reff]]></dc:creator><pubDate>Sun, 13 Oct 2013 18:34:18 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Sun, 13 Oct 2013 18:45:53 GMT]]></title><description><![CDATA[<p>reff reff schrieb:</p>
<blockquote>
<p>Tim06TR schrieb:</p>
<blockquote>
<ol start="3">
<li>A besitzt für die komplette Lebenszeit immer dasselbe B.</li>
</ol>
</blockquote>
<p>Bist du sicher? Wäre es nicht sinnvoll, ein swap/operator= anzubieten?</p>
</blockquote>
<p>Nach 10 Minuten nachdenken: Das würde vermutlich tausendmal mehr Ärger einhandeln, als es mir nützt.</p>
<p>EDIT: B könnte theoretisch auch ein Singleton sein (was es nicht ist), das würde nichts ändern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360025</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Sun, 13 Oct 2013 18:45:53 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Sun, 13 Oct 2013 19:08:09 GMT]]></title><description><![CDATA[<p>Nimm einen Pointer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360030</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 13 Oct 2013 19:08:09 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 07:19:17 GMT]]></title><description><![CDATA[<p>Wieso keine Referenz?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360096</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360096</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 14 Oct 2013 07:19:17 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 07:45:58 GMT]]></title><description><![CDATA[<p>Wieso eine Referenz?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360102</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 14 Oct 2013 07:45:58 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 08:25:50 GMT]]></title><description><![CDATA[<p>Schönere Syntax (keine Dereferenzierungspfeile) und man sagt damit aus, dass das Ding immer zu existieren hat (kein &quot;if (!ptr) ...&quot;)? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360111</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Mon, 14 Oct 2013 08:25:50 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 08:31:52 GMT]]></title><description><![CDATA[<p>Dobi schrieb:</p>
<blockquote>
<p>Schönere Syntax (keine Dereferenzierungspfeile)</p>
</blockquote>
<p>Eben nicht! Das Ding ist fremd und soll auch fremd aussehen. Vermeidet Fehler.</p>
<p>Dobi schrieb:</p>
<blockquote>
<p>und man sagt damit aus, dass das Ding immer zu existieren hat (kein &quot;if (!ptr) ...&quot;)? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
</blockquote>
<p>Komisch, if(!ptr) mache ich ich bei Zeigern nicht. Wenn der Benutzer Nullzeiger reinsteckt, ist er selber schuld, fertig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360112</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 14 Oct 2013 08:31:52 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 08:43:55 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Wenn der Benutzer Nullzeiger reinsteckt, ist er selber schuld, fertig.</p>
</blockquote>
<p>Kann man nicht in Konstruktor oder setter einfach eine Referenz fordern und dann einfach die Adresse zu nutzen? Wenn man einen Zeiger übergeben kann wirkt das etwas missverständlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360116</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360116</guid><dc:creator><![CDATA[Flashput]]></dc:creator><pubDate>Mon, 14 Oct 2013 08:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 10:03:54 GMT]]></title><description><![CDATA[<p>Flashput schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Wenn der Benutzer Nullzeiger reinsteckt, ist er selber schuld, fertig.</p>
</blockquote>
<p>Kann man nicht in Konstruktor oder setter einfach eine Referenz fordern und dann einfach die Adresse zu nutzen? Wenn man einen Zeiger übergeben kann wirkt das etwas missverständlich.</p>
</blockquote>
<p>Kann man auch.<br />
Aber dann WIRD bald jemand schreiben</p>
<pre><code>B b;
   return new A(b);
</code></pre>
<p>während er und jeder andere Leser bei</p>
<pre><code>B b;
   return new A(&amp;b);
</code></pre>
<p>sofort merkt, daß da was faul ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360146</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 14 Oct 2013 10:03:54 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 10:13:30 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Dobi schrieb:</p>
<blockquote>
<p>und man sagt damit aus, dass das Ding immer zu existieren hat (kein &quot;if (!ptr) ...&quot;)? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
</blockquote>
<p>Komisch, if(!ptr) mache ich ich bei Zeigern nicht. Wenn der Benutzer Nullzeiger reinsteckt, ist er selber schuld, fertig.</p>
</blockquote>
<p>Ja schon, aber ich dachte auch weniger an den Benutzer sondern an meinen Kollegen, der das Projekt vielleicht irgendwann übernimmt. Ich kann mir vorstellen, dass er bei nem Zeiger vielleicht denkt, dass das Ding auch Null sein könnte, wenn er meinen Kommentar an der Deklaration, der das als verboten beschreibt, nicht direkt liest. Bei einer Referenz hingegen wüsste er sofort Bescheid.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360150</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Mon, 14 Oct 2013 10:13:30 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 10:14:23 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Wieso keine Referenz?</p>
</blockquote>
<p>Weil man Referenzen nicht re-binden kann.<br />
Für noncopyable Objelte war das früher egal. Seit RValue-Referenzen und Move ist es aber auch da ein Thema.<br />
Referenzen sind also nur verwendbar wenn das Objekt noncopyable UND nonmovable sein soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360152</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 14 Oct 2013 10:14:23 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 10:18:32 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Kann man auch.<br />
Aber dann WIRD bald jemand schreiben</p>
<pre><code>B b;
   return new A(b);
</code></pre>
<p>während er und jeder andere Leser bei</p>
<pre><code>B b;
   return new A(&amp;b);
</code></pre>
<p>sofort merkt, daß da was faul ist.</p>
</blockquote>
<p>Kommt drauf an. Bei <code>unique_lock</code> übergibt man auch ne Referenz, und ich sehe keine Gefahr dass das jemand falsch versteht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360154</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 14 Oct 2013 10:18:32 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 10:20:34 GMT]]></title><description><![CDATA[<p>Dobi schrieb:</p>
<blockquote>
<p>Ich kann mir vorstellen, dass er bei nem Zeiger vielleicht denkt, dass das Ding auch Null sein könnte, wenn er meinen Kommentar an der Deklaration, der das als verboten beschreibt, nicht direkt liest. Bei einer Referenz hingegen wüsste er sofort Bescheid.</p>
</blockquote>
<p>Dann pack ein <code>assert(ptr);</code> als erste Zeile in den Konstruktor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360155</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 14 Oct 2013 10:20:34 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 13:12:31 GMT]]></title><description><![CDATA[<p>OK, ihr habt mich überzeugt. Ich hatte nämlich vor Kurzem einen ähnlichen Fall und hab mich für die Referenz entschieden. Hab jetzt aber die Umstellung auf Zeiger auf meine todo-Liste geschrieben. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360195</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Mon, 14 Oct 2013 13:12:31 GMT</pubDate></item><item><title><![CDATA[Reply to ref, ptr oder shared_ptr on Mon, 14 Oct 2013 16:47:21 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Wieso keine Referenz?</p>
</blockquote>
<p>Weil man Referenzen nicht re-binden kann.<br />
Für noncopyable Objelte war das früher egal. Seit RValue-Referenzen und Move ist es aber auch da ein Thema.<br />
Referenzen sind also nur verwendbar wenn das Objekt noncopyable UND nonmovable sein soll.</p>
</blockquote>
<p>Vor allem das überzeugt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360252</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 14 Oct 2013 16:47:21 GMT</pubDate></item></channel></rss>