Skip to main content
Fixed variable name in comment!
Source Link
Daniel Earwicker
  • 117k
  • 38
  • 209
  • 289

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class.

class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c = 'a'; c <= 'z'; c++) letters_.push_back(c); } // provide some way to get at foo_letters_ }; class Elsewhere { static StaticStuff staticStuff; // constructor runs once, single instance }; 

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class.

class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c = 'a'; c <= 'z'; c++) letters_.push_back(c); } // provide some way to get at foo_ }; class Elsewhere { static StaticStuff staticStuff; // constructor runs once, single instance }; 

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class.

class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c = 'a'; c <= 'z'; c++) letters_.push_back(c); } // provide some way to get at letters_ }; class Elsewhere { static StaticStuff staticStuff; // constructor runs once, single instance }; 
Source Link
Daniel Earwicker
  • 117k
  • 38
  • 209
  • 289

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class.

class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c = 'a'; c <= 'z'; c++) letters_.push_back(c); } // provide some way to get at foo_ }; class Elsewhere { static StaticStuff staticStuff; // constructor runs once, single instance };