<?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[Nodes: rekursiver Funktionsaufruf]]></title><description><![CDATA[<p>Hallo,<br />
habe mal eine Frage zu einem Problem, bei dem ich gerade auf dem Schlauch stehe...</p>
<p>Ich habe ein Basisinterface, IControl. Man kann nun beliebig viele Controls erstellen, u.A. auch Nodes. Somit sollen Ebenen möglich sein, nach dem Motto:</p>
<p>Kategorie 1 -&gt; Subkategorie 1 -&gt; Control 1<br />
Kategorie 2 -&gt; Control 2</p>
<p>Problem ist nun - wie kriege ich von ganz oben heraus (also vom obersten Node), wie tief das maximal gehen kann?</p>
<p>Ich dachte an so etwas, wie bei INode eine Funktion mit &quot;int GetMaxDepth&quot; zu implementieren. Dazu hat jedes IControl dann noch die Funktion &quot;bool HasDepth()&quot; (damit das ganze dynamisch bleibt). Doch was muss GetMaxDepth jetzt tun, um herauszufinden, wie tief zum Beispiel Kategorie 1 geht?</p>
<p>Es ist vermutlich einfach; und ich finde die Lösung einfach nicht?!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/289707/nodes-rekursiver-funktionsaufruf</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 23:51:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289707.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Jul 2011 11:43:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Nodes: rekursiver Funktionsaufruf on Mon, 11 Jul 2011 11:43:24 GMT]]></title><description><![CDATA[<p>Hallo,<br />
habe mal eine Frage zu einem Problem, bei dem ich gerade auf dem Schlauch stehe...</p>
<p>Ich habe ein Basisinterface, IControl. Man kann nun beliebig viele Controls erstellen, u.A. auch Nodes. Somit sollen Ebenen möglich sein, nach dem Motto:</p>
<p>Kategorie 1 -&gt; Subkategorie 1 -&gt; Control 1<br />
Kategorie 2 -&gt; Control 2</p>
<p>Problem ist nun - wie kriege ich von ganz oben heraus (also vom obersten Node), wie tief das maximal gehen kann?</p>
<p>Ich dachte an so etwas, wie bei INode eine Funktion mit &quot;int GetMaxDepth&quot; zu implementieren. Dazu hat jedes IControl dann noch die Funktion &quot;bool HasDepth()&quot; (damit das ganze dynamisch bleibt). Doch was muss GetMaxDepth jetzt tun, um herauszufinden, wie tief zum Beispiel Kategorie 1 geht?</p>
<p>Es ist vermutlich einfach; und ich finde die Lösung einfach nicht?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091337</guid><dc:creator><![CDATA[theliquidwave]]></dc:creator><pubDate>Mon, 11 Jul 2011 11:43:24 GMT</pubDate></item><item><title><![CDATA[Reply to Nodes: rekursiver Funktionsaufruf on Mon, 11 Jul 2011 11:55:34 GMT]]></title><description><![CDATA[<p>Straight forward: Rekursiv die Hierachie durchlaufen und dabei die tiefste Hierachiestufe bestimmen. Könnte etwa so aussehen:</p>
<pre><code class="language-cpp">unsigned int IControl::get_depth() const
{
   unsigned int Depth = 0;
   foreach( Child in Children )
   {
      Depth = max( Depth, Child.get_depth() );
   }
   return Depth +1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2091343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091343</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Mon, 11 Jul 2011 11:55:34 GMT</pubDate></item><item><title><![CDATA[Reply to Nodes: rekursiver Funktionsaufruf on Mon, 11 Jul 2011 12:08:32 GMT]]></title><description><![CDATA[<p>Ich sehe 2 Möglichkeiten:<br />
a) Jedes IControl soll gefragt werden können, wie tief ein eventueller Subbaum geht.<br />
b) Nur Nodes sollen gefragt werden können.</p>
<p>a):</p>
<pre><code class="language-cpp">class IControl
{
public: 
  virtual unsigned getSubControlDepth()
  {
    return 1; //default: nur dieses Control
  }
};

class INode : public IControl
{
  vector&lt;IControl*&gt; children;
public:
  virtual unsigned getSubControlDepth()
  {
    return 1 + std::accumulate(children.begin(), children.end(), 0u, 
      [](unsigned old, IControl* rhs){
        return std::max(old, rhs-&gt;getSubControlDepth());
    })
  }
};
</code></pre>
<p>b):</p>
<pre><code class="language-cpp">class INode : public IControl
{
  vector&lt;IControl*&gt; children;
public:
  unsigned getSubControlDepth()
  {
    if (children.empty()) return 1;
    return 1 + std::accumulate(children.begin(), children.end(), 1u, 
      [](unsigned old, IControl* rhs) -&gt; unsigned {
        INode* pNode = dynamic_cast&lt;INode*&gt;(rhs); 
        unsigned sub = pNode ? std::max(old, pNode-&gt;getSubControlDepth()) : old;
    })
  }
};
</code></pre>
<p>Erstmal nur so hingerotzt - geht sicher auch eleganter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091345</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 11 Jul 2011 12:08:32 GMT</pubDate></item><item><title><![CDATA[Reply to Nodes: rekursiver Funktionsaufruf on Mon, 11 Jul 2011 15:04:13 GMT]]></title><description><![CDATA[<p>So einfach... Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091412</guid><dc:creator><![CDATA[theliquidwave]]></dc:creator><pubDate>Mon, 11 Jul 2011 15:04:13 GMT</pubDate></item></channel></rss>