<?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[Codevereinfachung]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe momentan so etwas in meinem Code:</p>
<pre><code class="language-cpp">MyClass *CreateClass(const std::string&amp; str)
{
 if (str == &quot;blub&quot;)
 {
  return new MyClassBlub();
 }
 else if (str == &quot;bla&quot;)
 {
  return new MyClassBla();
 }
 else if (str == &quot;foo&quot;)
 {
  return new MyClassFoo();
 }

 // ...

 return NULL;
}
</code></pre>
<p>Das klappt zwar perfekt, aber es ist natürlich nicht so &quot;schön&quot;. Gibt es eine Möglichkeit das ganze über ein Array zu machen?</p>
<p>Dann bräuchte ich nicht bei jeder Erweiterung ein neues &quot;else if&quot; einfügen.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/247299/codevereinfachung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 14:57:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/247299.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 08 Aug 2009 23:44:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Codevereinfachung on Sat, 08 Aug 2009 23:44:47 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe momentan so etwas in meinem Code:</p>
<pre><code class="language-cpp">MyClass *CreateClass(const std::string&amp; str)
{
 if (str == &quot;blub&quot;)
 {
  return new MyClassBlub();
 }
 else if (str == &quot;bla&quot;)
 {
  return new MyClassBla();
 }
 else if (str == &quot;foo&quot;)
 {
  return new MyClassFoo();
 }

 // ...

 return NULL;
}
</code></pre>
<p>Das klappt zwar perfekt, aber es ist natürlich nicht so &quot;schön&quot;. Gibt es eine Möglichkeit das ganze über ein Array zu machen?</p>
<p>Dann bräuchte ich nicht bei jeder Erweiterung ein neues &quot;else if&quot; einfügen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1757703</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1757703</guid><dc:creator><![CDATA[asdasdasdasdasd]]></dc:creator><pubDate>Sat, 08 Aug 2009 23:44:47 GMT</pubDate></item><item><title><![CDATA[Reply to Codevereinfachung on Sun, 09 Aug 2009 00:05:26 GMT]]></title><description><![CDATA[<p>Da gibts viele Wege. Vielleicht eine map von string nach Funktionszeiger.</p>
<pre><code class="language-cpp">//Datei MyClassBlub.cpp
MyClassBlub::MyClassBlubb()
{
  ...
};
MyClassBlub* MyClassBlub::create()//static in MyClassBlub
{
  return new MyClassBlub;
}
static bool registerClassRegisterBoolFlagHolderWorkaroundHack=MyClass::registerClass(&quot;blubb&quot;,&amp;MyClassBlub::create);
</code></pre>
<pre><code class="language-cpp">//MyClass.cpp
bool MyClass::registerClass(string const&amp; str,MyClass*(*c)())
{
  theRegisterClassMap[str]=c;
}
MyClass* MyClass::CreateClass(const std::string&amp; str)
{
  MyClass*(*c)()=theRegisterClassMap[str];
  if(!c)
    return 0;
  return (*c)();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1757711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1757711</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 09 Aug 2009 00:05:26 GMT</pubDate></item><item><title><![CDATA[Reply to Codevereinfachung on Sun, 09 Aug 2009 00:17:57 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1536196.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-p-is-1536196.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1757713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1757713</guid><dc:creator><![CDATA[Caster]]></dc:creator><pubDate>Sun, 09 Aug 2009 00:17:57 GMT</pubDate></item><item><title><![CDATA[Reply to Codevereinfachung on Sun, 09 Aug 2009 00:22:04 GMT]]></title><description><![CDATA[<p>Man muss sich allerdings immer überlegen was besser ist. Dein If-Zweig ist vielleicht nicht sehr ästhetisch, aber jeder wird die Funktion sofort ohne Probleme verstehen und Nachteile bei der Erweiterung hast du auch keine, in beiden Fällen müsstest du irgendwo die neue Klasse hinzufügen. Bei den If-Zweig bedarf das keiner großen Dokumentation um das hinzubekommen, bei einem komplizierteren Setup sieht das schon anders aus.</p>
<p>In C++ brauche ich so etwas normal nicht, aber in C verwende ich folgendes Idiom (auf deinen Fall umgestrickt):</p>
<pre><code class="language-cpp">Basis* create(const char *str)
{
   static const struct {
      const char *name;
      Basis *(*create_it)();
   } creators[] = {
       { &quot;bla&quot;, create_bla },
       { &quot;foo&quot;, create_foo },
       { NULL, NULL }
   }, *cur = &amp;creators[0];

   while(cur-&gt;name)
     if(!strcmp(cur-&gt;name, str))
        return cur-&gt;create_it();
   return NULL;     
}
</code></pre>
<p>Ist zwar noch sehr C-lastig, aber die Idee ist wohl klar: einfach ein Array mit String + Funktion zum erstellen und dann per Schleife durchgehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1757714</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1757714</guid><dc:creator><![CDATA[Tippgeber]]></dc:creator><pubDate>Sun, 09 Aug 2009 00:22:04 GMT</pubDate></item></channel></rss>