<?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[Vritual Methode und call by value]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

class A
{
public:
  virtual void call()
  {
    cout &lt;&lt; &quot;Call A&quot; &lt;&lt; endl;
  }
};

class B : public A
{
public:
  virtual void call()
  {
    cout &lt;&lt; &quot;Call B&quot; &lt;&lt; endl;
    A::call ();
  }
};

class Container
{
  A value;

public:

  Container(const A&amp; a) : value(a) {}

  void callValue() { value.call (); }
};

/*
 * 
 */
int
main (int argc, char** argv)
{

  B b;
  b.call ();
  cout &lt;&lt; &quot;..................&quot; &lt;&lt; endl;
  A&amp; a = b;
  a.call ();
  cout &lt;&lt; &quot;..................&quot; &lt;&lt; endl;
  Container c(b);
  c.callValue ();
}
</code></pre>
<p>Ausgabe:</p>
<pre><code>Call B
Call A
..................
Call B
Call A
..................
Call A
</code></pre>
<p>Wieso wird beim dritten mal nur die Methode der Klasse A aufgerufen. Ich kann mir das nicht erklären.</p>
<pre><code>g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
</code></pre>
<p>Danke, Thomas</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/284446/vritual-methode-und-call-by-value</link><generator>RSS for Node</generator><lastBuildDate>Sat, 22 Aug 2026 02:27:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/284446.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Apr 2011 16:28:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 16:28:48 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

class A
{
public:
  virtual void call()
  {
    cout &lt;&lt; &quot;Call A&quot; &lt;&lt; endl;
  }
};

class B : public A
{
public:
  virtual void call()
  {
    cout &lt;&lt; &quot;Call B&quot; &lt;&lt; endl;
    A::call ();
  }
};

class Container
{
  A value;

public:

  Container(const A&amp; a) : value(a) {}

  void callValue() { value.call (); }
};

/*
 * 
 */
int
main (int argc, char** argv)
{

  B b;
  b.call ();
  cout &lt;&lt; &quot;..................&quot; &lt;&lt; endl;
  A&amp; a = b;
  a.call ();
  cout &lt;&lt; &quot;..................&quot; &lt;&lt; endl;
  Container c(b);
  c.callValue ();
}
</code></pre>
<p>Ausgabe:</p>
<pre><code>Call B
Call A
..................
Call B
Call A
..................
Call A
</code></pre>
<p>Wieso wird beim dritten mal nur die Methode der Klasse A aufgerufen. Ich kann mir das nicht erklären.</p>
<pre><code>g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
</code></pre>
<p>Danke, Thomas</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043331</guid><dc:creator><![CDATA[Siassei]]></dc:creator><pubDate>Fri, 01 Apr 2011 16:28:48 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 16:33:45 GMT]]></title><description><![CDATA[<p>Im Container-Konstruktor kopierst du das den Parameter in das A-Objekt &quot;value&quot; - dabei gehen auch alle Eigenschaften der abgeleiteten Klassen verloren. Wenn du Polymorphie nutzen willst, mußt du im Container eine Referenz oder einen Zeiger verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043334</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 01 Apr 2011 16:33:45 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 16:40:32 GMT]]></title><description><![CDATA[<p>Siehe dafür auch &quot;Object Slicing&quot; Problem.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043336</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043336</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Fri, 01 Apr 2011 16:40:32 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 18:21:34 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>... Wenn du Polymorphie nutzen willst, mußt du im Container eine Referenz oder einen Zeiger verwenden.</p>
</blockquote>
<p>Ich denke ich kenne die Antwort, aber dennoch ...<br />
Ich benutze also die Variable A&amp; value in der Klasse Container.</p>
<pre><code class="language-cpp">Container abc() {
  B b;
  Container c(b);
  return c;
}

void test() {
  Container con = abc();
  // ....
  con.callValue();
}
</code></pre>
<p>Beim verlassen von abc() wird b zerstört und der Speicher freigegeben. Ist der Wert der Variable value im Container noch gültig? Oder ist es wie bei Zeigern, dass die Referenz nun auf einen freigegeben Speicher zeigt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043375</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043375</guid><dc:creator><![CDATA[Siassei]]></dc:creator><pubDate>Fri, 01 Apr 2011 18:21:34 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 18:27:38 GMT]]></title><description><![CDATA[<p>Siassei schrieb:</p>
<blockquote>
<p>Ist der Wert der Variable value im Container noch gültig?</p>
</blockquote>
<p>Ja, denn oben hast du in Zeile 33 eine Kopie gemacht.</p>
<blockquote>
<p>Oder ist es wie bei Zeigern, dass die Referenz nun auf einen freigegeben Speicher zeigt?</p>
</blockquote>
<p>Das kann schon passieren, wenn du Referenzen auf lokale Objekte zurück gibst. Das ist jedoch meistens recht leicht zu diagnostizieren für den Compiler, wenn du Warnungen aktivierst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043382</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043382</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 01 Apr 2011 18:27:38 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 18:36:12 GMT]]></title><description><![CDATA[<p>Siassei schrieb:</p>
<blockquote>
<p>CStoll schrieb:</p>
<blockquote>
<p>... Wenn du Polymorphie nutzen willst, mußt du im Container eine Referenz oder einen Zeiger verwenden.</p>
</blockquote>
<p>Ich denke ich kenne die Antwort, aber dennoch ...<br />
Ich benutze also die Variable A&amp; value in der Klasse Container.</p>
<pre><code class="language-cpp">Container abc() {
  B b;
  Container c(b);
  return c;
}

void test() {
  Container con = abc();
  // ....
  con.callValue();
}
</code></pre>
</blockquote>
<p>Wenn du Container so verwendest wie in deinem Eröffnungsbeitrag verwendest, hast du im Container eine Kopie des lokalen Objekts in c - diese wird nach dem Verlassen der Funktion in con kopiert.<br />
Mit der Anpassung &quot;A**&amp;** value;&quot; hättest du ein Problem, weil dein Container eine Referenz auf etwas aufhebt, was irgendwann mal eine gültige Variable war (was inzwischen dort steht, weiß vermutlich nichtmal dein Compiler).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043389</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 01 Apr 2011 18:36:12 GMT</pubDate></item><item><title><![CDATA[Reply to Vritual Methode und call by value on Fri, 01 Apr 2011 18:37:21 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Mit der Anpassung &quot;A**&amp;** value;&quot; hättest du ein Problem, weil dein Container eine Referenz auf etwas aufhebt, was irgendwann mal eine gültige Variable war (was inzwischen dort steht, weiß vermutlich nichtmal dein Compiler).</p>
</blockquote>
<p>Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2043392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2043392</guid><dc:creator><![CDATA[Siassei]]></dc:creator><pubDate>Fri, 01 Apr 2011 18:37:21 GMT</pubDate></item></channel></rss>