{"id":994,"date":"2011-10-26T10:50:01","date_gmt":"2011-10-26T16:50:01","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=994"},"modified":"2011-10-26T10:50:01","modified_gmt":"2011-10-26T16:50:01","slug":"is-this-a-safe-pattern-for-auto-registration","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2011\/10\/26\/is-this-a-safe-pattern-for-auto-registration\/","title":{"rendered":"Is this a safe pattern for auto-registration?"},"content":{"rendered":"<p>Hey,<\/p>\n<p>I&#8217;m trying to have various components in my game pre-register themselves upon startup of the game.  I&#8217;m using this pattern:<\/p>\n<p>Here&#8217;s my registration code that is in one .cpp file.  The register_init() function is available from anywhere:<\/p>\n<pre>\r\nstd::vector<Init*> &get_init() {\r\n    static std::vector<Init*> goods;\r\n    return goods;\r\n}\r\nbool register_init(Init *v) {\r\n    get_init().push_back(v);\r\n    return true;\r\n}\r\n<\/pre>\n<p>Here&#8217;s how I auto-register some init code from some other file by calling register init to initialize a variable:<\/p>\n<pre>\r\nint _is_levedit_init = register_init(new LeveditInit());\r\n<\/pre>\n<p>Is there some case where this would NOT work or is somehow a really bad idea?  Or is there some pattern that is better to use?<\/p>\n<p>-Phil<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, I&#8217;m trying to have various components in my game pre-register themselves upon startup of the game. I&#8217;m using this pattern: Here&#8217;s my registration code that is in one .cpp file. The register_init() function is available from anywhere: std::vector &#038;get_init() { static std::vector goods; return goods; } bool register_init(Init *v) { get_init().push_back(v); return true; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-994","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=994"}],"version-history":[{"count":1,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/994\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/994\/revisions\/995"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}