Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider the similarities between each node of the graph that you describe and create a class for them (class GraphElement below). It should encapsulate its relationship to its child nodes, and it should do something locally with any incoming messages (function localAction). You should then derive classes that represent specific variations - such as the image generator you mention - and change the local action. Each class may take a copy of the original message, or change it as you need. </p> <p>In my example code here I have create the default graph node - GraphNode - and made it simply print incoming messages before passing them to its child nodes. I have used a string object for the incoming message - a lot nicer than a plain old C char * array [example: you can derive a string from char * when message2 is created in the code below]. I have made those object const references as its cheap, fast, and never changes the original.</p> <p>I have derived a class called CatNode as an example of the variation you need. Objects of this type contain a history of all messages, and print out that history when a new message arrives. Not very useful - but a good example none the less. This demonstrates how each node may do anything to a copy of the original message - rewrite localAction(). It also passes that history to any child nodes - rewrite incomingMessage with a change to the parameter passed to deliverMessage().</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;string&gt; using std::cout; using std::endl; using std::vector; using std::string; class GraphNode { public: GraphNode( string &amp; name ) : mChildren(), mName(name) {} GraphNode( const char * const name ) : mChildren(), mName(name==NULL?"":name) {} virtual void incomingMessage( const string &amp; str ) { localAction(str); // This node is to do something. deliverMessage(str); // Child nodes are to do things too. } void addChild( GraphNode * child ) { mChildren.push_back( child ); } protected: // Rewrite this function for child classes who are to do different things with messages. virtual void localAction( const string &amp; str ) { cout &lt;&lt; mName &lt;&lt; " : " &lt;&lt; str &lt;&lt; endl; } void deliverMessage( const string &amp; str ) { vector&lt;GraphNode*&gt;::iterator itr = mChildren.begin(); for( ; itr != mChildren.end(); ++itr ) (*itr)-&gt;incomingMessage(str); } // Data members vector&lt;GraphNode*&gt; mChildren; string mName; }; // [ GraphNode ] class CatNode : public GraphNode { public: CatNode( string &amp; name ) : GraphNode(name), mHistory() {} CatNode( const char * const name ) : GraphNode(name), mHistory() {} virtual void incomingMessage( const string &amp; str ) { localAction(str); deliverMessage(mHistory); } protected: virtual void localAction( const string &amp; str ) { mHistory += str; cout &lt;&lt; mName &lt;&lt; " : " &lt;&lt; mHistory &lt;&lt; endl; } // Data members string mHistory; }; // [ CatNode ] main() { // root -&gt; childA GraphNode root("Root Node"); CatNode childA("Child A"); root.addChild( &amp;childA ); root.incomingMessage("Message1"); cout &lt;&lt; endl; // root -&gt; childA -&gt; childC // \-&gt; childB GraphNode childB("Child B"); root.addChild( &amp;childB ); GraphNode childC("Child C"); childA.addChild( &amp;childC ); string message2("Message2"); root.incomingMessage(message2); } // [ main ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload