   Need a new "plug-in" mechanism
  I have classes that need to "register" themselves with another class, but they need to do so automatically without a code prompt.Context:I have a resource manager that can load resources for an entire class with this syntax:manager->LoadResourcesFor("Bullet"); // exampleThe manager knows about the "Bullet" class because "Bullet" registered itself as a resource client earlier. It did so with some macro extratrickery that creates a static function in a class that returns a bool:bool NameOfClass::RegisterMyself() {   ResourceManager->Register("NameOfClass");   return true;   }static bool NameOfClass::bla = RegisterMyself();So when the program runs, since the bool is a static it runs the RegisterMyself() before main() is ever run. Since this happens so early, the resource manager itself also has to be up and running before main. What happens is a small cascade of objects having to initialize themselves on first use. This is bad.Is there a better way to create a "plug in" system? I need my objects to register themselves so that i can create new objects with new code which register themselves with the manager (which is in a separate library, so you can't update the code for it). Most often, these are usually implemented with static objects because they get created by the compiler. I'm trying to avoid this behavior.Thanks for your advice.   
  Since you asked in the other thread...Personally, I probably wouldn't have objects auto-register themselves. It kind of limits what you can do if you ever have to use a different library or if you only want to include certain ones, or conditionally include some depending on what hardware the user has.But, when I had to do something like this, I used this sort of construct:template <typename T>class TypeRegistry<T>{private:    static vector<TypeRegistry<T> *> Registry;protected:    virtual void Registration(T &Target){}public:    void    FinaliseRegistration(T &Target){   typename vector<TypeRegistry<T> >::iterator    it;    for(it=Registry.begin();it!=Registry.end();++it){  (*it)->Registration(Target);  delete *it;   }   Registry.erase(Registry.begin(),Registry.end());   Registry.swap(Registry);    }    TypeRegistry(){   Registry.push_back(this);    }};TypeRegistry<manager>::vector<TypeRegistry<manager> *>  TypeRegistry<manager>::Registry;TypeRegistry<manager> ManagerRegistry;template <typename T, typename R>class RegisteredType<T,R>: public TypeRegistry<T>{private:protected:    virtual voidRegistration(T &Target){   // For your example.   R::RegisterMyself(); // Others might be T.Add(...); T.Add<R>(...);}public:};// Then the Registration is more like:class foo{...};RegisteredType<manager,foo> *FooType=new RegisteredType<manager,foo>(/*optional params*/);// And then simply in main():// Setup manager.ManagerRegistry.FinaliseRegistration(manager);Not too automatic, uses too many globals for most peoples' liking, and is too roundabout on memory management. And *technically* I think it still yields undefined behavior since you can't guarantee that the vector gets created before other RegisteredTypes. There probably is some template black magic way to do it though I'd guess.Like I said, I don't care for or strive towards auto-registration any more (though I also don't use c++ much either). It's simpiler, more maintainable, more flexible, and plays nicer with other code to register things manually or do some post-build work to "automate" things rather than doing compiler trickery.   