<?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[auf const Methode non const Methode aufrufen]]></title><description><![CDATA[<p>Mahlzeit,</p>
<p>ich habe folgenden Code:</p>
<pre><code class="language-cpp">void Mngr::renderPatches(const Camera&amp; cam) const {
   for(int i=0; i &lt; sharedPatchData-&gt;visiblePatches.size(); i++)
      sharedPatchData-&gt;visiblePatches[i]-&gt;render(cam);
}
</code></pre>
<p>sharedPatchData ist vom Typ SharedPatchData*. visiblePatches ist vom Typ Patch** und render() ist NICHT const!<br />
Was ich nicht verstehe: Wieso kann ich aus einer const Methode (renderPatches) eine NICHT const Methode (render) aufrufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/245751/auf-const-methode-non-const-methode-aufrufen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 13:08:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/245751.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 18 Jul 2009 10:15:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to auf const Methode non const Methode aufrufen on Sat, 18 Jul 2009 10:15:58 GMT]]></title><description><![CDATA[<p>Mahlzeit,</p>
<p>ich habe folgenden Code:</p>
<pre><code class="language-cpp">void Mngr::renderPatches(const Camera&amp; cam) const {
   for(int i=0; i &lt; sharedPatchData-&gt;visiblePatches.size(); i++)
      sharedPatchData-&gt;visiblePatches[i]-&gt;render(cam);
}
</code></pre>
<p>sharedPatchData ist vom Typ SharedPatchData*. visiblePatches ist vom Typ Patch** und render() ist NICHT const!<br />
Was ich nicht verstehe: Wieso kann ich aus einer const Methode (renderPatches) eine NICHT const Methode (render) aufrufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745186</guid><dc:creator><![CDATA[thomasrrr]]></dc:creator><pubDate>Sat, 18 Jul 2009 10:15:58 GMT</pubDate></item><item><title><![CDATA[Reply to auf const Methode non const Methode aufrufen on Sat, 18 Jul 2009 10:56:07 GMT]]></title><description><![CDATA[<p>thomasrrr schrieb:</p>
<blockquote>
<p>Was ich nicht verstehe: Wieso kann ich aus einer const Methode (renderPatches) eine NICHT const Methode (render) aufrufen?</p>
</blockquote>
<p>Weil <code>visiblePatches[i]</code> in jedem Fall ein Zeiger auf nicht-const ist. Innerhalb einer const-Elementfunktion sind nur die &quot;direkten&quot; Objektmitglieder const -- sofern sie nicht mit <code>mutable</code> deklariert worden sind.</p>
<pre><code class="language-cpp">class foo {
  int i;
  int&amp; r;
  int* p;
public:
  foo(int&amp; r, int* p) : i(42), r(r), p(p) {}
  void bar() const {
    ++i;  // NICHT erlaubt, i ist const
    ++p;  // NICHT erlaubt, p ist const
    ++r;  // OK
    ++*p; // OK
  }
};
</code></pre>
<p>Diese Regeln sind am sinnvollsten für die Mehrheit. Es gibt natürlich Fälle, in denen einem das &quot;zuwenig const&quot; ist. Beispiel: vector</p>
<pre><code class="language-cpp">struct int_vector {
  int* begin;
  int* end;
  int* capacity;
  .....
};
</code></pre>
<p>Da die Elemente logisch gesehen Bestandteile der Klasse sind, muss man hier Hand anlegen: Zugriff schützen und Elementfunktionen überladen:</p>
<pre><code class="language-cpp">struct int_vector {
private:
  int* begin;
  int* end;
  int* capacity;
public:
  int      &amp; operator[](int index)       {return begin[index];}
  int const&amp; operator[](int index) const {return begin[index];}
  .....
};
</code></pre>
<p>Beachte: <code>begin[index]</code> ist auch beim zweiten Mal nicht-const. Die Referenz wird bei der Rückgabe implizit zu einer ref-to-const konvertiert, siehe Rückgabetyp.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745206</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sat, 18 Jul 2009 10:56:07 GMT</pubDate></item></channel></rss>