c++ - What alternatives do I have to a global static object constructor -


i have solution lets me add functionality microcontroller project (adding and) including files. whole initializatin of hardware happens automagically header file included. approach based on (the constructor of) global static object:

main.cpp:

#include "led.h"  // <-- led gets initialized automagically  int main ()   {     led::turnon ();     // ...     led::turnoff ();   } 

led.h:

class led   {   public:     led () noexcept;    public:     static void turnon ();     static void turnoff ();   }; 

led.cpp:

/* compiler calls constructor before first use of method in    compilation unit  */ led theled;  led::led () noexcept  // <-- shall not throw   {     // initialize led   }  void led::turnon ()   {     // turn led on   }  void led::turnoff ()   {     // turn led off   } 

i wonder, whether singleton / factory method approach should used. both seem imply lazy initialization (and therefore not deterministic @ runtime) or need programmer explicitly call initialization method. (i know example non-deterministic well)

so question: "what alternatives have achieve similar behavior?" , probably: standard approach initialize hardware in embedded projects (that need no de-initialization of hardware)?

edit: corrected misleading wording


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -