Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would separate the conversion of strings to enum into a distinct object. This can easily be solved by a map btw. But error handling etc. is still something which neither D nor the factory should be worried about.</p> <p>Then either D calls the converter to get its enum, or it is already converted beforehand, so D only needs to pass the enum to the factory. (Btw the factory would better use a map too instead of a switch internally).</p> <p>This raises the question: do you actually need the enums at all (in places other than D and the factory)? If not, maybe the enum could be left out of the picture and you could use a map to convert directly from strings to types (i.e. - since C++ doesn't support dynamic class loading - to function objects which create the necessary concrete type instances for you). A rough example (I don't have an IDE to test it so bear with me if there are any errors in it):</p> <pre><code>// Function type returning a pointer to B typedef (B*)(*func)() StaticConstructor; // Function creating instances of subclass E B* createSubclassE() { return new E(...); } // Function creating instances of subclass F B* createSubclassF() { return new F(...); } // Mapping from strings to constructor methods creating specific subclasses of B map&lt;string, StaticConstructor&gt; factoryMap; factoryMap["E"] = &amp;createSubclassE; factoryMap["F"] = &amp;createSubclassF; </code></pre> <p>Of course, the created instances should also be disposed of properly - in production code, the returned objects could be e.g. enclosed in an <code>auto_ptr</code>. But I hope this short example is enough to show you the basic idea. Here is <a href="http://www.newty.de/fpt/index.html" rel="nofollow noreferrer">a tutorial</a> if you want more...</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