<?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[variadic factory]]></title><description><![CDATA[<p>Hey ich hab es öfter das ich ne factory brauche um eine überschaubare menge an objekte mit gleicher base class zu aus strings zu erstellen. Wenn die alle das gleiche interface zum erstellen haben geht das auch super, aber ich dachte mir ich probier das mal mit variadic templates (unterschiedlichem interface), komm da aber nicht so richtig weiter :D.</p>
<pre><code class="language-cpp">struct Base {

};
struct One : public Base {
	One(int a, std::string b)
		: a(a), b(b)
	{}
	int a; std::string b;
};
struct Two : public Base {
	Two(One a, double b)
		: a(a), b(b)
	{}
	One a; double b;
};

// perfekt wäre:
	Factory&lt;Base&gt; fact;
	fact.Register&lt;One&gt;(&quot;one&quot;);
	fact.Register&lt;Two&gt;(&quot;two&quot;);
	Base *one = fact.Create(&quot;one&quot;, 1, &quot;hello&quot;);
	Base *two = fact.Create(&quot;two&quot;, One(2, &quot;muh&quot;), 1.337);
</code></pre>
<p>Es stockt beim abspeichern/aufrufen mit variadic templates, mit ist immer die template signatur im weg um das in nen container zu packen, steh da voll auf dem schlauch. Hab schon einiges durchgelesen aber komm nicht wirklich weiter. Hoffe ihr könnt mir helfen <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/332762/variadic-factory</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 13:47:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332762.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 May 2015 20:10:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to variadic factory on Wed, 20 May 2015 20:10:02 GMT]]></title><description><![CDATA[<p>Hey ich hab es öfter das ich ne factory brauche um eine überschaubare menge an objekte mit gleicher base class zu aus strings zu erstellen. Wenn die alle das gleiche interface zum erstellen haben geht das auch super, aber ich dachte mir ich probier das mal mit variadic templates (unterschiedlichem interface), komm da aber nicht so richtig weiter :D.</p>
<pre><code class="language-cpp">struct Base {

};
struct One : public Base {
	One(int a, std::string b)
		: a(a), b(b)
	{}
	int a; std::string b;
};
struct Two : public Base {
	Two(One a, double b)
		: a(a), b(b)
	{}
	One a; double b;
};

// perfekt wäre:
	Factory&lt;Base&gt; fact;
	fact.Register&lt;One&gt;(&quot;one&quot;);
	fact.Register&lt;Two&gt;(&quot;two&quot;);
	Base *one = fact.Create(&quot;one&quot;, 1, &quot;hello&quot;);
	Base *two = fact.Create(&quot;two&quot;, One(2, &quot;muh&quot;), 1.337);
</code></pre>
<p>Es stockt beim abspeichern/aufrufen mit variadic templates, mit ist immer die template signatur im weg um das in nen container zu packen, steh da voll auf dem schlauch. Hab schon einiges durchgelesen aber komm nicht wirklich weiter. Hoffe ihr könnt mir helfen <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454257</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454257</guid><dc:creator><![CDATA[foofel]]></dc:creator><pubDate>Wed, 20 May 2015 20:10:02 GMT</pubDate></item><item><title><![CDATA[Reply to variadic factory on Thu, 21 May 2015 14:17:25 GMT]]></title><description><![CDATA[<p>Wo ist da jetzt der Vorteil zu dem hier?</p>
<pre><code class="language-cpp">Base *one = new One(1, &quot;hello&quot;);
    Base *two = new Two(One(2, &quot;muh&quot;), 1.337);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2454321</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454321</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 21 May 2015 14:17:25 GMT</pubDate></item><item><title><![CDATA[Reply to variadic factory on Thu, 21 May 2015 14:43:08 GMT]]></title><description><![CDATA[<p>Naja, man benutzt eine Factory, um halt dynamisch im Hintergrund entscheiden zu können, was anderes zurück zu geben. Z.B. weil sich eine Situation oder Konfiguration geändert hat, dann muss man den Aufrufer-Code nicht anfassen. (nur an einer Stelle in der Factory)<br />
Z.B. durch nachladen einer DLL, kann sich das ändern.</p>
<pre><code>// in anderer DLL
class SpezialOne : public One {};
fact.Register&lt;SpezialOne&gt;(&quot;one&quot;); 

// alter Client-Code
Base *one = fact.Create(&quot;one&quot;, 1, &quot;hello&quot;); // gibt SpezialOne zurück
</code></pre>
<p>Aber das war glaube ich nicht sein Problem. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>EDIT: ich glaube, ich habe Unsinn geschrieben... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454328</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454328</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Thu, 21 May 2015 14:43:08 GMT</pubDate></item><item><title><![CDATA[Reply to variadic factory on Thu, 21 May 2015 21:04:42 GMT]]></title><description><![CDATA[<p>foofel schrieb:</p>
<blockquote>
<p>Es stockt beim abspeichern/aufrufen mit variadic templates, mit ist immer die template signatur im weg um das in nen container zu packen, steh da voll auf dem schlauch. Hab schon einiges durchgelesen aber komm nicht wirklich weiter. Hoffe ihr könnt mir helfen <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="🙂"
    /></p>
</blockquote>
<p>So wie Du Dir die Factory vorstellst, brauchst Du Laufzeitpolymorphie.<br />
z.B. &quot;Interfaces&quot; zum Erstellen der Subklassen in Base:</p>
<pre><code>struct Base 
{
 virtual Base* create(int a, std::string b) { throw &quot;njet&quot;; }
 virtual Base* create(int a, std::string b, double c) { throw &quot;njet&quot;; } // für Two
};
</code></pre>
<p>Und dann einfach eine map&lt;string, Base*&gt; in der Factory...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454362</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454362</guid><dc:creator><![CDATA[__cu]]></dc:creator><pubDate>Thu, 21 May 2015 21:04:42 GMT</pubDate></item></channel></rss>