Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I referred to this article to implement a self-registering class factory similar to the one described in TimW's answer, but it has the nice trick of using a templated factory proxy class to handle the object registration. Well worth a look :)</p> <p>Self-Registering Objects in C++ -> <a href="http://www.ddj.com/184410633" rel="nofollow noreferrer">http://www.ddj.com/184410633</a></p> <p><strong>Edit</strong></p> <p>Here's the test app I did (tidied up a little ;):</p> <p>object_factory.h</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; // Forward declare the base object class class Object; // Interface that the factory uses to communicate with the object proxies class IObjectProxy { public: virtual Object* CreateObject() = 0; virtual std::string GetObjectInfo() = 0; }; // Object factory, retrieves object info from the global proxy objects class ObjectFactory { public: static ObjectFactory&amp; Instance() { static ObjectFactory instance; return instance; } // proxies add themselves to the factory here void AddObject(IObjectProxy* object) { objects_.push_back(object); } size_t NumberOfObjects() { return objects_.size(); } Object* CreateObject(size_t index) { return objects_[index]-&gt;CreateObject(); } std::string GetObjectInfo(size_t index) { return objects_[index]-&gt;GetObjectInfo(); } private: std::vector&lt;IObjectProxy*&gt; objects_; }; // This is the factory proxy template class template&lt;typename T&gt; class ObjectProxy : public IObjectProxy { public: ObjectProxy() { ObjectFactory::Instance().AddObject(this); } Object* CreateObject() { return new T; } virtual std::string GetObjectInfo() { return T::TalkToMe(); }; }; </code></pre> <p>objects.h</p> <pre><code>#include &lt;iostream&gt; #include "object_factory.h" // Base object class class Object { public: virtual ~Object() {} }; class ClassA : public Object { public: ClassA() { std::cout &lt;&lt; "ClassA Constructor" &lt;&lt; std::endl; } ~ClassA() { std::cout &lt;&lt; "ClassA Destructor" &lt;&lt; std::endl; } static std::string TalkToMe() { return "This is ClassA"; } }; class ClassB : public Object { public: ClassB() { std::cout &lt;&lt; "ClassB Constructor" &lt;&lt; std::endl; } ~ClassB() { std::cout &lt;&lt; "ClassB Destructor" &lt;&lt; std::endl; } static std::string TalkToMe() { return "This is ClassB"; } }; </code></pre> <p>objects.cpp</p> <pre><code>#include "objects.h" // Objects get registered here ObjectProxy&lt;ClassA&gt; gClassAProxy; ObjectProxy&lt;ClassB&gt; gClassBProxy; </code></pre> <p>main.cpp</p> <pre><code>#include "objects.h" int main (int argc, char * const argv[]) { ObjectFactory&amp; factory = ObjectFactory::Instance(); for (int i = 0; i &lt; factory.NumberOfObjects(); ++i) { std::cout &lt;&lt; factory.GetObjectInfo(i) &lt;&lt; std::endl; Object* object = factory.CreateObject(i); delete object; } return 0; } </code></pre> <p>output:</p> <pre><code>This is ClassA ClassA Constructor ClassA Destructor This is ClassB ClassB Constructor ClassB Destructor </code></pre>
 

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