<?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[ein polymorphes Problem]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich hab ein schwer zu ergoogelndes Problem mit Polymorphie. Vielleicht kann mir jemand weiterhelfen. Folgender Beispiel-Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Data
{
	public:
		virtual ~Data() {}
		int x;
};

class Verifier
{
	public:
		virtual bool verify(Data* d) = 0; 
};

class specialData : public Data
{
	public:
		int y;
};

class specialVerifierA : public Verifier
{
	public:
		bool verify(Data* d)
		{
			cout &lt;&lt; &quot;:-/ specialVerifierA &quot;&lt;&lt; d-&gt;x &lt;&lt;&quot; &gt;= 0 ?&quot;&lt;&lt;endl;
			return (d -&gt; x &gt;= 0);
		}
		bool verify(specialData* s)
		{
			cout &lt;&lt; &quot;:-) specialVerifierA &quot;&lt;&lt; s-&gt;x &lt;&lt;&quot; &gt;= 0 &amp;&amp; &quot; &lt;&lt; s-&gt;y &lt;&lt; &quot; &gt;= 0 ?&quot;&lt;&lt;endl;
			return (s -&gt; x &gt;= 0 &amp;&amp; s -&gt; y &gt;= 0);
		}
};

class specialVerifierB : public Verifier
{
	public:
		bool verify(Data* d)
		{
			cout &lt;&lt; &quot;:-/ specialVerifierB &quot;&lt;&lt; d-&gt;x &lt;&lt;&quot; &lt; 100 ?&quot;&lt;&lt;endl;
			return (d -&gt; x &lt; 100);
		}
		bool verify(specialData* s)
		{
			cout &lt;&lt; &quot;:-) specialVerifierB &quot;&lt;&lt; s-&gt;x &lt;&lt;&quot; &lt; 100 &amp;&amp; &quot; &lt;&lt; s-&gt;y &lt;&lt; &quot; &lt; 100 ?&quot;&lt;&lt;endl;
			return (s -&gt; x &lt; 100 &amp;&amp; s -&gt; y &lt; 100);
		}
};

class Checker
{
	private:
		Verifier* v;
		Verifier* w;

	public:
		Checker(Verifier* vv, Verifier* ww) {v = vv; w = ww;}
		bool check(Data* d)
		{
			specialData* s = dynamic_cast&lt;specialData*&gt;(d);
			if(s)
			{
				cout &lt;&lt; &quot;checking specialData&quot; &lt;&lt; endl;
				return (v -&gt; verify(s) &amp;&amp; w -&gt; verify(s));
			}
			else
			{
				cout &lt;&lt; &quot;checking Data&quot; &lt;&lt; endl;
				return (v -&gt; verify(d) &amp;&amp; w -&gt; verify(d));
			}
		}
};

int main()
{
	specialVerifierA A;
	specialVerifierB B;

	Checker C(&amp;A, &amp;B);
	specialData S;
	S.x = 23;
	S.y = 42;
	cout &lt;&lt; ( C.check(&amp;S) ? &quot;jo&quot; : &quot;ne&quot; ) &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Die Ausgabe ist:</p>
<pre><code>checking specialData
:-/ specialVerifierA 23 &gt;= 0 ?
:-/ specialVerifierB 23 &lt; 100 ?
jo
</code></pre>
<p>Das bedeutet, er ruft beim <code>check</code> en immer die <code>verify(Data* d)</code> auf. Ich hätte aber gern, dass er die <code>verify(specialData* s)</code> aufruft - immerhin ist das zu- <code>verify</code> ende Objekt ja vom Typ <code>specialData</code> , wie die erste Zeile der Ausgabe verlauten lässt.</p>
<p>Nun könnte ich für jeden von <code>Data</code> abgeleiteten Typ eine solche Zeile</p>
<pre><code class="language-cpp">virtual bool verify(class specialData* d) {}
</code></pre>
<p>in die <code>Verifier</code> Klasse einbauen, aber es gibt eine ganze Reihe solcher abgeleiteten Typen, und das macht irgendwie die schöne &quot;Allgemeingültigkeit&quot; des Codes kaput.</p>
<p>Hat jemand Vorschläge, was ich alternativ tun könnte?</p>
<p>Lars</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/284116/ein-polymorphes-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 22 Aug 2026 07:42:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/284116.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Mar 2011 23:08:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ein polymorphes Problem on Fri, 25 Mar 2011 23:08:59 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich hab ein schwer zu ergoogelndes Problem mit Polymorphie. Vielleicht kann mir jemand weiterhelfen. Folgender Beispiel-Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Data
{
	public:
		virtual ~Data() {}
		int x;
};

class Verifier
{
	public:
		virtual bool verify(Data* d) = 0; 
};

class specialData : public Data
{
	public:
		int y;
};

class specialVerifierA : public Verifier
{
	public:
		bool verify(Data* d)
		{
			cout &lt;&lt; &quot;:-/ specialVerifierA &quot;&lt;&lt; d-&gt;x &lt;&lt;&quot; &gt;= 0 ?&quot;&lt;&lt;endl;
			return (d -&gt; x &gt;= 0);
		}
		bool verify(specialData* s)
		{
			cout &lt;&lt; &quot;:-) specialVerifierA &quot;&lt;&lt; s-&gt;x &lt;&lt;&quot; &gt;= 0 &amp;&amp; &quot; &lt;&lt; s-&gt;y &lt;&lt; &quot; &gt;= 0 ?&quot;&lt;&lt;endl;
			return (s -&gt; x &gt;= 0 &amp;&amp; s -&gt; y &gt;= 0);
		}
};

class specialVerifierB : public Verifier
{
	public:
		bool verify(Data* d)
		{
			cout &lt;&lt; &quot;:-/ specialVerifierB &quot;&lt;&lt; d-&gt;x &lt;&lt;&quot; &lt; 100 ?&quot;&lt;&lt;endl;
			return (d -&gt; x &lt; 100);
		}
		bool verify(specialData* s)
		{
			cout &lt;&lt; &quot;:-) specialVerifierB &quot;&lt;&lt; s-&gt;x &lt;&lt;&quot; &lt; 100 &amp;&amp; &quot; &lt;&lt; s-&gt;y &lt;&lt; &quot; &lt; 100 ?&quot;&lt;&lt;endl;
			return (s -&gt; x &lt; 100 &amp;&amp; s -&gt; y &lt; 100);
		}
};

class Checker
{
	private:
		Verifier* v;
		Verifier* w;

	public:
		Checker(Verifier* vv, Verifier* ww) {v = vv; w = ww;}
		bool check(Data* d)
		{
			specialData* s = dynamic_cast&lt;specialData*&gt;(d);
			if(s)
			{
				cout &lt;&lt; &quot;checking specialData&quot; &lt;&lt; endl;
				return (v -&gt; verify(s) &amp;&amp; w -&gt; verify(s));
			}
			else
			{
				cout &lt;&lt; &quot;checking Data&quot; &lt;&lt; endl;
				return (v -&gt; verify(d) &amp;&amp; w -&gt; verify(d));
			}
		}
};

int main()
{
	specialVerifierA A;
	specialVerifierB B;

	Checker C(&amp;A, &amp;B);
	specialData S;
	S.x = 23;
	S.y = 42;
	cout &lt;&lt; ( C.check(&amp;S) ? &quot;jo&quot; : &quot;ne&quot; ) &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Die Ausgabe ist:</p>
<pre><code>checking specialData
:-/ specialVerifierA 23 &gt;= 0 ?
:-/ specialVerifierB 23 &lt; 100 ?
jo
</code></pre>
<p>Das bedeutet, er ruft beim <code>check</code> en immer die <code>verify(Data* d)</code> auf. Ich hätte aber gern, dass er die <code>verify(specialData* s)</code> aufruft - immerhin ist das zu- <code>verify</code> ende Objekt ja vom Typ <code>specialData</code> , wie die erste Zeile der Ausgabe verlauten lässt.</p>
<p>Nun könnte ich für jeden von <code>Data</code> abgeleiteten Typ eine solche Zeile</p>
<pre><code class="language-cpp">virtual bool verify(class specialData* d) {}
</code></pre>
<p>in die <code>Verifier</code> Klasse einbauen, aber es gibt eine ganze Reihe solcher abgeleiteten Typen, und das macht irgendwie die schöne &quot;Allgemeingültigkeit&quot; des Codes kaput.</p>
<p>Hat jemand Vorschläge, was ich alternativ tun könnte?</p>
<p>Lars</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2040212</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2040212</guid><dc:creator><![CDATA[Lawilog]]></dc:creator><pubDate>Fri, 25 Mar 2011 23:08:59 GMT</pubDate></item><item><title><![CDATA[Reply to ein polymorphes Problem on Fri, 25 Mar 2011 23:17:21 GMT]]></title><description><![CDATA[<p>Schau dir vielleicht mal den Visitor bzw. Double Dispatch Pattern an...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2040215</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2040215</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 25 Mar 2011 23:17:21 GMT</pubDate></item><item><title><![CDATA[Reply to ein polymorphes Problem on Fri, 25 Mar 2011 23:17:56 GMT]]></title><description><![CDATA[<p>verify(Data*) und verify(specialData*) sind zwei vollkommen unabhängige Funktionen. Insbesondere hat verify(specialData*) in specialVerifier nichts mit verify(Data*) in Verifier zu tun. Deshalb wird sie gar nicht berücksichtigt. Darüber hinaus werden Funktionsüberladungen immer schon zur Compilezeit durch die statischen Typen der Objekte aufgelöst.</p>
<p>Die Lösung ist aber einfach: teste in deinen specialVerifier::verify(data*), ob der Parameter nicht eigentlich ein specialData* ist und rufe in dem Fall die überladene Version manuell auf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2040216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2040216</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Fri, 25 Mar 2011 23:17:56 GMT</pubDate></item><item><title><![CDATA[Reply to ein polymorphes Problem on Fri, 25 Mar 2011 23:47:41 GMT]]></title><description><![CDATA[<p>ipsec schrieb:</p>
<blockquote>
<p>Die Lösung ist aber einfach: teste in deinen specialVerifier::verify(data*), ob der Parameter nicht eigentlich ein specialData* ist und rufe in dem Fall die überladene Version manuell auf.</p>
</blockquote>
<p>Brilliant! <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="🙂"
    /> (Das lässt sich auf mein echtes Programm übertragen.) Ein Hoch auf das Forum! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2040223</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2040223</guid><dc:creator><![CDATA[Lawilog]]></dc:creator><pubDate>Fri, 25 Mar 2011 23:47:41 GMT</pubDate></item></channel></rss>