Note that there are some explanatory texts on larger screens.

plurals
  1. POElegant way to implement extensible factories in C++
    primarykey
    data
    text
    <p>I am looking for an intuitive and extensible way to implement factories for subclasses of a given base class in <a href="/questions/tagged/c%2b%2b" class="post-tag" title="show questions tagged &#39;c++&#39;" rel="tag">c++</a>. I want to provide such a factory function in a library.The tricky part is that I want said factory to work for user-defined subclasses as well (e.g. having the library's factory function build different subclasses depending on what modules are linked to it). The goal is to have minimal burden/confusion for downstream developers to use the factories.</p> <p>An example of what I want to do is: given a <code>std::istream</code>, construct and return an object of whatever subclass matches the content, or a null pointer if no matches are found. The global factory would have a signature like:</p> <pre><code>Base* Factory(std::istream &amp;is){ ... }; </code></pre> <p>I am familiar with <a href="http://en.wikipedia.org/wiki/Prototype_pattern" rel="nofollow noreferrer">prototype factories</a>, but I prefer to avoid the need to make/store prototype objects. A related question is posted here for <a href="/questions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a>: <a href="https://stackoverflow.com/questions/16251932/allowing-maximal-flexibly-extensibility-using-a-factory">Allowing maximal flexibly/extensibility using a factory</a>.</p> <p>I am not looking for <a href="/questions/tagged/c%2b%2b11" class="post-tag" title="show questions tagged &#39;c++11&#39;" rel="tag">c++11</a>-specific solutions at the moment, but if they are more elegant I would be happy to learn about those.</p> <p>I came up with one working solution which I believe is fairly elegant, which I will post as an answer. I can imagine this problem to be fairly common, so I am wondering if anyone knows of better approaches. </p> <p><strong>EDIT</strong>: it seems some clarification is in order...</p> <p>The idea is for the factory to construct an object of a derived class, without containing the logic to decide which one. To make matters worse, the factory method will end up as part of a library and derived classes may be defined in plugins. </p> <p>Derived classes must be able to decide for themselves whether or not they are fit for construction, based on the input provided (for example an input file). This decision can be implemented as a predicate that can be used by the factory, as was suggested by several people (great suggestion, by the way!).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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