Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store an "object type" in a std::map?
    text
    copied!<p>Long introduction, question is at the end:</p> <hr> <p>Assume I have a base class that's creating an interface</p> <pre><code>class base { public: virtual ~base(); virtual void calc( int* variables ) = 0; } </code></pre> <p>and a few classes that are inherited that do the work (only two are shown here):</p> <pre><code>class add : public base { const int a, b, c; public: add( int a_, int b_, int c_ ) : a(a_), b(b_), c(c_) {} void calc( int* variables ) { variables[a] = variables[b] + variables[c]; } } class inc : public base { const int a; public: inc( int a_ ) : a(a_) {} void calc( int* variables ) { variables[a]++; } } </code></pre> <p>and finally some code that's using this construct:</p> <pre><code>base* task[2]; task[0] = new add( 0, 1, 2 ); task[1] = new inc( 3 ); int data[4]; /* ... */ for( int i = 0; i &lt; 2; i++ ) task[i]-&gt;calc( data ); </code></pre> <p>That's working so far - but it's defining my tasks during compile time. This should be changed to runtime by parsing an input file. Assume the parsing has been done and in <code>std::string</code> variable <code>command</code> is the object type (like <code>add</code> or <code>inc</code>) and in a <code>std::vector&lt;int&gt; params</code> are the parameters for the constructor.</p> <p>Now I could have a long list of</p> <pre><code>if( command.compare( "add" ) ) task[end] = new add( params[0], params[1], params[2] ); else if( command.compare( "inc" ) ) task[end] = new inc( params[0] ); else /... */ </code></pre> <p>apart from becoming quite unreadable that's just a linear seach. So in the spirit of <a href="https://stackoverflow.com/questions/650162/why-switch-statement-cannot-be-applied-on-strings">Why switch statement cannot be applied on strings?</a> I'd like to replace that linear search by a <code>std::map</code> (or hash map...).</p> <p>So after this long introduction I can finally come to the question:</p> <hr> <p><strong>How can I define and fill a <code>std::map</code> so that references(?) to an object are stored in such a way that I can create late on the object dynamically out of that informations?</strong></p> <p>So with the code above I'd like to do something that might look like this in the end:</p> <pre><code>// define and fill std::map&lt; std::sting, ???? &gt; lookup; lookup["add"] = add; lookup["inc"] = inc; /* ... */ // use: while( linesInConfigAvailable ) { /* ... parse ... */ switch( params.size() ) { case 1: task[end] = new lookup[command]( params[0] ); break; case 3: task[end] = new lookup[command]( params[0], params[1], params[2] ); break; } } </code></pre> <p>PS: So far I didn't need RTTI in my code. It'd be nice if that could stay so...</p>
 

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