<?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[const referenz Rückgabe bei getter für nicht fundamentalen Typ]]></title><description><![CDATA[<pre><code class="language-cpp">class person {
public:
  person : name(&quot;&quot;) {} 
  std::string getName() const {return name;} // Version 1
  const std::string&amp; getName() const {return name;}  // Version 2
private:
  std::string name;
};
</code></pre>
<p>Gibt es einen Grund Version 1 des Getters gegenüber Version 2 zu bevorzugen?</p>
<p>Gruß,<br />
maber</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224076/const-referenz-rückgabe-bei-getter-für-nicht-fundamentalen-typ</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 00:51:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224076.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 05 Oct 2008 12:12:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const referenz Rückgabe bei getter für nicht fundamentalen Typ on Sun, 05 Oct 2008 12:12:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class person {
public:
  person : name(&quot;&quot;) {} 
  std::string getName() const {return name;} // Version 1
  const std::string&amp; getName() const {return name;}  // Version 2
private:
  std::string name;
};
</code></pre>
<p>Gibt es einen Grund Version 1 des Getters gegenüber Version 2 zu bevorzugen?</p>
<p>Gruß,<br />
maber</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593665</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593665</guid><dc:creator><![CDATA[maber]]></dc:creator><pubDate>Sun, 05 Oct 2008 12:12:36 GMT</pubDate></item><item><title><![CDATA[Reply to const referenz Rückgabe bei getter für nicht fundamentalen Typ on Sun, 05 Oct 2008 12:24:28 GMT]]></title><description><![CDATA[<p>Die zweite Variante ist nicht mehr möglich, wenn sich die interne Representation des Typs ändert. Z.B.:</p>
<pre><code class="language-cpp">class Person
{
public:
    // ...
    std::string getName() { return std::string(vorName).append(nachName); }
private:
    std::string vorName, nachName;
}
</code></pre>
<p>Die zweite Variante mag performanter sein, gibt aber auch einen Teil der Kapselung auf, da bei der Rückgabe einer Referenz, das Objekt ja erhalten bleiben muss. Folglich also (meistens) eine Membervariable ist. Sofern man davon ausgehen kann, dass sich die Implementation eines Typs nicht mehr ändert, kann man natürlich die zweite Variante verwenden (imho).</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593675</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593675</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sun, 05 Oct 2008 12:24:28 GMT</pubDate></item></channel></rss>