<?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[Klassenaufruf basierend auf String]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich würde gerne folgendes machen:<br />
In einem XML-File habe ich eine Liste von Klassen, die alle auf dem selben Interface basieren.<br />
Jetzt möchte ich während der Laufzeit unterschiedliche Klassen aufrufen.<br />
Also z.B. (pseudo-code):</p>
<pre><code>MyInterface * x;

// So mache ich es im Moment (1):
if (xmlfile-&gt;getClassName() == &quot;Class1&quot;) x = new Class1();
if (xmlfile-&gt;getClassName() == &quot;Class2&quot;) x = new Class2();

//So hätte ich es gerne (2):
x = new xmlfile-&gt;getClassName().toClass();
</code></pre>
<p>Geht sowas irgendwie in C++. In Java ging sowas problemlos...</p>
<p>Danke,</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/129135/klassenaufruf-basierend-auf-string</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 01:42:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/129135.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Dec 2005 16:34:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 16:34:45 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich würde gerne folgendes machen:<br />
In einem XML-File habe ich eine Liste von Klassen, die alle auf dem selben Interface basieren.<br />
Jetzt möchte ich während der Laufzeit unterschiedliche Klassen aufrufen.<br />
Also z.B. (pseudo-code):</p>
<pre><code>MyInterface * x;

// So mache ich es im Moment (1):
if (xmlfile-&gt;getClassName() == &quot;Class1&quot;) x = new Class1();
if (xmlfile-&gt;getClassName() == &quot;Class2&quot;) x = new Class2();

//So hätte ich es gerne (2):
x = new xmlfile-&gt;getClassName().toClass();
</code></pre>
<p>Geht sowas irgendwie in C++. In Java ging sowas problemlos...</p>
<p>Danke,</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939015</guid><dc:creator><![CDATA[pixsta]]></dc:creator><pubDate>Sat, 10 Dec 2005 16:34:45 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 16:35:50 GMT]]></title><description><![CDATA[<p>Jo gibts auch. Mit operator typeid.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939016</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sat, 10 Dec 2005 16:35:50 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 16:49:36 GMT]]></title><description><![CDATA[<p>ich glaube, dass wir von 2 unterschiedlichen sachen reden. was ich meine ist, dass ich quasi aus einem string heraus eine klasse initilisiere.</p>
<p>also xml-&gt;getClassName() hat nur einen String und und die toClass methode macht irgendwie sowas:</p>
<p>x = new Class(&quot;xml-&gt;getClassNameAsString()&quot;);</p>
<p>Mir fällt jetzt aus dem kopf heraus nicht ein, wie das in java ging, aber in PHP sieht das z.b. so aus:</p>
<pre><code>$x = &quot;Class1&quot;;

$y = new $$x; // Erzeugt eine neue Instanz der Klasse Class1
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/939024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939024</guid><dc:creator><![CDATA[pixsta]]></dc:creator><pubDate>Sat, 10 Dec 2005 16:49:36 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 16:52:39 GMT]]></title><description><![CDATA[<p>In C++ hast du keinen Laufzeitzugriff auf solche Meta-Informationen wie Klassennamen und deren Methoden, etc. Das ist zB in Java anders.</p>
<p>Eine Möglichkeit wäre zB eine Factory zu bauen:</p>
<pre><code class="language-cpp">BaseClass* CreateDerived (const char* name)
{
    if(strcmp(&quot;DerivedA&quot;,name)==0)
        return new DerivedA();

    return new DerivedB();
}
</code></pre>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939028</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Sat, 10 Dec 2005 16:52:39 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 17:01:48 GMT]]></title><description><![CDATA[<p>- nix -</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939035</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939035</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sat, 10 Dec 2005 17:01:48 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 17:03:42 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894"><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894">@FireFlow</a></a> - in dem falle kann es ned zur compile-time feststehen ...<br />
/edit<br />
<a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894"><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894">@FireFlow</a></a> - einfach wegmachen ist feige ;P</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939040</guid><dc:creator><![CDATA[ronny]]></dc:creator><pubDate>Sat, 10 Dec 2005 17:03:42 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 18:06:25 GMT]]></title><description><![CDATA[<p>wie schon erwähnt, funktioniert das so nicht</p>
<pre><code class="language-cpp">x = new xmlfile-&gt;getClassName().toClass();
</code></pre>
<p>so gehts in der realität:</p>
<pre><code class="language-cpp">//eine Beispielfunktion
X* createClass1(){
    return new Class1();
}

//vorbereitungen
std::map&lt;std::string,X*(*)()&gt; create;
create.push_back(std::make_pair(&quot;Class1&quot;,createClass1));

//aufruf
X* x=create[xmlfile-&gt;getClassName()]();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/939089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939089</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Sat, 10 Dec 2005 18:06:25 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenaufruf basierend auf String on Sat, 10 Dec 2005 18:18:52 GMT]]></title><description><![CDATA[<p>danke. das hilft mir sehr weiter, da es einige dinge etwas vereinfacht und weniger redundant (vom code her) macht. <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/939102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939102</guid><dc:creator><![CDATA[pixsta]]></dc:creator><pubDate>Sat, 10 Dec 2005 18:18:52 GMT</pubDate></item></channel></rss>