<?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[rückgabetyp string]]></title><description><![CDATA[<p>Hallo!</p>
<pre><code class="language-cpp">string test()
{
	string s (&quot;ABC&quot;);
	return s;
}

int main() 
{ 
	string t = test();
	return 0;
}
</code></pre>
<p>Die Funktion test erzeugt ein Objekt vom Typ string und gibt dieses Objekt zurück.<br />
Ist das so machbar, oder ist das falsch, weil das Objekt lokal erzeugt wurde?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/245316/rückgabetyp-string</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 21:14:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/245316.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 12 Jul 2009 18:12:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:12:56 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<pre><code class="language-cpp">string test()
{
	string s (&quot;ABC&quot;);
	return s;
}

int main() 
{ 
	string t = test();
	return 0;
}
</code></pre>
<p>Die Funktion test erzeugt ein Objekt vom Typ string und gibt dieses Objekt zurück.<br />
Ist das so machbar, oder ist das falsch, weil das Objekt lokal erzeugt wurde?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741629</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741629</guid><dc:creator><![CDATA[frag0r]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:12:56 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:15:47 GMT]]></title><description><![CDATA[<p>dein lokal erzeugter string in test() wird in string t bei der RÜckgabe KOPIERT.<br />
Daher ist es egal ob string s noch weiter existiert oder nicht.<br />
Alles korrekt soweit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741634</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741634</guid><dc:creator><![CDATA[Beantworter]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:15:47 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:15:55 GMT]]></title><description><![CDATA[<p>Richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741635</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741635</guid><dc:creator><![CDATA[Kóyaánasqatsi]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:15:55 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:18:07 GMT]]></title><description><![CDATA[<p>Korrekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741637</guid><dc:creator><![CDATA[Nachahmer]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:18:07 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:23:50 GMT]]></title><description><![CDATA[<p>frag0r schrieb:</p>
<blockquote>
<p>Die Funktion test erzeugt ein Objekt vom Typ string und gibt dieses Objekt zurück. Ist das so machbar, oder ist das falsch, weil das Objekt lokal erzeugt wurde?</p>
</blockquote>
<p>Das kann man so machen, ja. Falsch ist es nicht. Beantworter sagte, dass der string kopiert wird. Das stimmt allerdings nicht so ganz. Es kommt auf Deinen Compiler drauf an, ob er &quot;copy elisions&quot; durchführen kann. Mein G++ 4.3.3 Compiler erzeugt genau ein string-Objekt direkt in t und da wird <em>gar nichts</em> kopiert. Garantiert wird das allerdings vom C++ Standard auch nicht. Bei einem string ist das aber echt noch egal. Ich würd's so lassen.</p>
<p>Ich hatte auch neulich darüber gebloggt. Wen's interessiert:<br />
<a href="http://pizer.wordpress.com/2009/06/25/c-exploiting-copy-elisions/#more-462" rel="nofollow">C++: Exploiting Copy Elisions</a></p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741639</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:23:50 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 18:55:52 GMT]]></title><description><![CDATA[<p>Sebastian Pizer schrieb:</p>
<blockquote>
<p>...Mein G++ 4.3.3 Compiler erzeugt genau ein string-Objekt direkt in t ...</p>
</blockquote>
<p>Ich denke, da liegt der entscheidende Punkt! Soweit ich weiß, garantiert der Standard, dass das Ergebnis so wäre, als ob eine Kopie angefertigt würde.</p>
<p>Das angesprochene Problem (Übergabe eines lokalen Objekts) ist durchaus real und relevant ... aber eben nur bei Referenzen und Pointern (und ähnlichen &quot;Verweisstrukturen&quot; wie Handleobjekten, IDs, ....).<br />
Schon ein einziges &quot;&amp;&quot; hätte da den Unterschied gemacht... und vielleicht kommt der Fragesteller ja auch aus Java und &quot;sieht&quot; überall Referenzen, wo keine sind. <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>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741656</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 12 Jul 2009 18:55:52 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 19:08:29 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Das angesprochene Problem (Übergabe eines lokalen Objekts) ist durchaus real und relevant ... aber eben nur bei Referenzen und Pointern (und ähnlichen &quot;Verweisstrukturen&quot; wie Handleobjekten, IDs, ....).</p>
</blockquote>
<p>Ja, jetzt erinnere ich mich wieder, die Referenzen!<br />
Ist schon ein paar Jahre her, meine C++ Praxis!<br />
<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>
<p>Danke an alle,<br />
Gruß,<br />
f.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741669</guid><dc:creator><![CDATA[frag0r]]></dc:creator><pubDate>Sun, 12 Jul 2009 19:08:29 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 19:12:46 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Sebastian Pizer schrieb:</p>
<blockquote>
<p>...Mein G++ 4.3.3 Compiler erzeugt genau ein string-Objekt direkt in t ...</p>
</blockquote>
<p>Ich denke, da liegt der entscheidende Punkt! Soweit ich weiß, garantiert der Standard, dass das Ergebnis so wäre, als ob eine Kopie angefertigt würde.</p>
</blockquote>
<p>Der Standard erlaubt explizit das Weglassen von Kopien in bestimmten Kontexten -- auch <em>dann</em>, wenn Konstruktoren und Destruktoren Seiteneffekte haben.</p>
<p>Nachlesen kann man das im Standard oder in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf" rel="nofollow">N1905.pdf</a>, Kapitel 12.8, Absatz 15. (Das ist ein auf dem C++03 Standard basierender C++0x Entwurf, wo praktisch noch kein C++0x enthalten ist).</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741676</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741676</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sun, 12 Jul 2009 19:12:46 GMT</pubDate></item><item><title><![CDATA[Reply to rückgabetyp string on Sun, 12 Jul 2009 21:03:22 GMT]]></title><description><![CDATA[<p>Sebastian Pizer schrieb:</p>
<blockquote>
<p>...<br />
Der Standard erlaubt explizit das Weglassen von Kopien in bestimmten Kontexten -- auch <em>dann</em>, wenn Konstruktoren und Destruktoren Seiteneffekte haben....</p>
</blockquote>
<p>Das glaube ich ... aber das war nicht der Punkt, auf den ich hinauswollte (und den ich offensichtlich unzureichend verdeutlicht habe): Er fordert aber (egal, wie der Compiler das umsetzt), dass der Aufrufer hinterher ein gültiges/&quot;lebendes&quot; Objekt in den Händen hält.</p>
<p>Ich wollte auf die Lebenszeit hinaus, die ja beim Thema &quot;Rückgabe von temporären Objekten&quot; das Problem darstellt.</p>
<p>Aber gut, dass Du diesen Effekt noch dazu beigetragen hast - ich lerne immer viel aus Deinen fundierten und doch sehr verstädlichen Beiträgen - Danke! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1741739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1741739</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 12 Jul 2009 21:03:22 GMT</pubDate></item></channel></rss>