<?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[Collection abgeleiteter Klassen]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich rätsle jetzt schon ein par Stunden an nem Stück Code das ich unter <a href="http://www.devx.com/tips/Tip/15125" rel="nofollow">http://www.devx.com/tips/Tip/15125</a> gefunden hab rum.</p>
<blockquote>
<p>Say you want to have several classes-modifiers, and you want to execute a certain virtual function for each of them. The ideal solution is to create an abstract class with a purely virtual function, inherit your implementation classes from this base class and make a collection of all the implementation classes. The problem is that, the user needs to remember to create exactly one copy of each implementation class and correctly &quot;register&quot; it. The following example helps to solve this problem by automatically creating exactly one instance of each implementation of the class modifier:</p>
</blockquote>
<pre><code>#include &lt;vector&gt;
template &lt;class T&gt; class AutoMake
{
  virtual void trick() {t;} // virtual function uses static variable
  static T t;
};
template &lt;class T&gt; T AutoMake&lt;T&gt;::t;

template &lt;class T&gt;  class Modifier : AutoMake&lt;T&gt;
{
protected:
  Modifier() {vec.push_back(this);}
private:
  std::vector&lt;Modifier*&gt; vec;
};

class ModifierImpl1 : public Modifier&lt;ModifierImpl1&gt; {};
class ModifierImpl2 : public Modifier&lt;ModifierImpl2&gt; {};
</code></pre>
<blockquote>
<p>That's all: already in main(...) function you will have 2 pointers in<br />
Modifier::vec (with ModifierImpl1* and ModifierImpl2* types);</p>
</blockquote>
<p>Wenn ich den Sinn richtig verstehe sollten die abgeleiteten Klassen über den Vector zur Verfügung stehen, allerdings ist die größe immer 1.</p>
<pre><code>ModifierImpl1 a;
a.vec.size() //=1 (sichtbarkeit wurde auf public geändert)
</code></pre>
<p>Kann mir jemand sagen ob der Code völliger Quatsch ist, ich das ganze nicht richtig verwende oder ich das ganze falsch verstanden hab.<br />
Falls es kein Quatsch ist wäre ich für eine Erklärung des Codes dankbar.</p>
<p>Danke im Vorraus....</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/239477/collection-abgeleiteter-klassen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 12:20:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/239477.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Apr 2009 22:44:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Collection abgeleiteter Klassen on Thu, 23 Apr 2009 22:44:34 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich rätsle jetzt schon ein par Stunden an nem Stück Code das ich unter <a href="http://www.devx.com/tips/Tip/15125" rel="nofollow">http://www.devx.com/tips/Tip/15125</a> gefunden hab rum.</p>
<blockquote>
<p>Say you want to have several classes-modifiers, and you want to execute a certain virtual function for each of them. The ideal solution is to create an abstract class with a purely virtual function, inherit your implementation classes from this base class and make a collection of all the implementation classes. The problem is that, the user needs to remember to create exactly one copy of each implementation class and correctly &quot;register&quot; it. The following example helps to solve this problem by automatically creating exactly one instance of each implementation of the class modifier:</p>
</blockquote>
<pre><code>#include &lt;vector&gt;
template &lt;class T&gt; class AutoMake
{
  virtual void trick() {t;} // virtual function uses static variable
  static T t;
};
template &lt;class T&gt; T AutoMake&lt;T&gt;::t;

template &lt;class T&gt;  class Modifier : AutoMake&lt;T&gt;
{
protected:
  Modifier() {vec.push_back(this);}
private:
  std::vector&lt;Modifier*&gt; vec;
};

class ModifierImpl1 : public Modifier&lt;ModifierImpl1&gt; {};
class ModifierImpl2 : public Modifier&lt;ModifierImpl2&gt; {};
</code></pre>
<blockquote>
<p>That's all: already in main(...) function you will have 2 pointers in<br />
Modifier::vec (with ModifierImpl1* and ModifierImpl2* types);</p>
</blockquote>
<p>Wenn ich den Sinn richtig verstehe sollten die abgeleiteten Klassen über den Vector zur Verfügung stehen, allerdings ist die größe immer 1.</p>
<pre><code>ModifierImpl1 a;
a.vec.size() //=1 (sichtbarkeit wurde auf public geändert)
</code></pre>
<p>Kann mir jemand sagen ob der Code völliger Quatsch ist, ich das ganze nicht richtig verwende oder ich das ganze falsch verstanden hab.<br />
Falls es kein Quatsch ist wäre ich für eine Erklärung des Codes dankbar.</p>
<p>Danke im Vorraus....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1700484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1700484</guid><dc:creator><![CDATA[mamue110]]></dc:creator><pubDate>Thu, 23 Apr 2009 22:44:34 GMT</pubDate></item></channel></rss>