c++ - initalizer problem with plugins. a test -


i had longer more complicated question , have code http://www.ideone.com/veovp

however i'll simplify it. there wrong code below , there better way below?

i worried line std::list<plugin*>& plugins , how set while keeping reference. i'll let guys pick code apart.

#include <list> #include <string> class plugin{ public:     static std::list<plugin*>*plugins;     std::string name;     plugin(const std::string&n) : name(n)     {       static std::list<plugin*> plugins;       this->plugins=&plugins;       plugins.push_back(this);     } };  //main.cpp #include "plugin.h"  class plugin1 : public plugin{ public:     plugin1():plugin("1"){} };  static plugin1 plugin;   std::list<plugin*>* plugin::plugins; std::list<plugin*>& plugins = *plugin::plugins; //global name plz int main(){     for(auto c=plugins.cbegin(); c!=plugins.cend(); ++c) {         printf("%s\n", (*c)->name.c_str());     } }  //plugina.cpp #include "plugin.h"  class plugina : public plugin{ public:     plugina():plugin("a"){} };  static plugina plugin; 

that looks weird me. if goal have sort of global plugin container/manager there reason not use singleton pattern this:

class plugincontainer {     static plugincontainer& instance()      {          static plugincontainer* m_this = 0;         if(!m_this)               m_this = new plugincontainer;         return *m_this;     }      void register(plugin* plugin) { ... add list ... }     const list<plugin*>& plugins() const { ... return ... }      protected:         plugincontainer() {} };  class plugin{     public:     plugin(const std::string& n) : name(n)     {          plugincontainer::instance().register( );     }     private:     std::string name; }; 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -