Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best solution I can currently think of is by using a <code>Factory</code> class which stores pointers to producing functions for each derived class. When a new derived class is made, a function pointer to a producing method can be stored in the factory. </p> <p>Here is some code to illustrate my approach:</p> <pre class="lang-c prettyprint-override"><code>#include &lt;iostream&gt; #include &lt;vector&gt; class Base{}; // Factory class to produce Base* objects from an int (for simplicity). // The class uses a list of registered function pointers, which attempt // to produce a derived class based on the given int. class Factory{ public: typedef Base*(*ReadFunPtr)(int); private: static vector&lt;ReadFunPtr&gt; registeredFuns; public: static void registerPtr(ReadFunPtr ptr){ registeredFuns.push_back(ptr); } static Base* Produce(int value){ Base *ptr=NULL; for(vector&lt;ReadFunPtr&gt;::const_iterator I=registeredFuns.begin(),E=registeredFuns.end();I!=E;++I){ ptr=(*I)(value); if(ptr!=NULL){ return ptr; } } return NULL; } }; // initialize vector of funptrs std::vector&lt;Factory::ReadFunPtr&gt; Factory::registeredFuns=std::vector&lt;Factory::ReadFunPtr&gt;(); // An example Derived class, which can be produced from an int=0. // The producing method is static to avoid the need for prototype objects. class Derived : public Base{ private: static Base* ProduceDerivedFromInt(int value){ if(value==0) return new Derived(); return NULL; } public: Derived(){}; // registrar is a friend because we made the producing function private // this is not necessary, may be desirable (e.g. encapsulation) friend class DerivedRegistrar; }; // Register Derived in the Factory so it will attempt to construct objects. // This is done by adding the function pointer Derived::ProduceDerivedFromInt // in the Factory's list of registered functions. struct DerivedRegistrar{ DerivedRegistrar(){ Factory::registerPtr(&amp;(Derived::ProduceDerivedFromInt)); } } derivedregistrar; int main(){ // attempt to produce a Derived object from 1: should fail Base* test=Factory::Produce(1); std::cout &lt;&lt; test &lt;&lt; std::endl; // outputs 0 // attempt to produce a Derived object from 0: works test=Factory::Produce(0); std::cout &lt;&lt; test &lt;&lt; std::endl; // outputs an address } </code></pre> <p><strong>TL;DR</strong>: in this approach, downstream developers need to implement the producing function of a derived class as a <code>static</code> member function (or a non-member function) and register it in the factory using a simple <code>struct</code>.</p> <p>This seems simple enough and does not require any prototype objects.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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