0

I am trying to write simple logger class, and I ended up with singleton class design. Example bellow:

DataIO.h:

class DataIO
{
private:
   static DataIO instance;
public:
   static time_t date;
   static std::string path;
   static FILE* log;

private:
   DataIO()
   { 
      date = time(NULL);
      path = "#Output_" + std::to_string(date);
      std::string buffer = "mkdir " + path;
      system(buffer.c_str());
   };
   ~DataIO();
public:
   static DataIO getInstance() const { return instance; };
   static void restart();
};

DataIO.cpp:

time_t DataIO::date;
std::string DataIO::path;
FILE* DataIO::log;

DataIO DataIO::instance;

Do I have any guaranty about the order in with static fields will be initialized?? Usually i know i don't have any...
But in this case my DataIO constructor require that those three static fields must be declared before constructor is called. And therefore does it force the order in with static fields are declared??

PatrykB
  • 1,579
  • 1
  • 15
  • 24
  • This is not a real singleton design. Usually, you wouldn't have `date`, `path`, and `log` be static. They would be instance fields, and you would instantiate `instance` once, and have a static method return it. – Jerfov2 Mar 17 '17 at 15:41
  • Jerfov2 I know they should not be static from the point of view of singleton class design, but I want to call them `DataIO::date` not `getInstance().data` – PatrykB Mar 17 '17 at 15:43
  • Then what is the point of even having `instance`? – Jerfov2 Mar 17 '17 at 15:54
  • Jerfov2, i don't realy need access to DataIO object, but i need this object to be created at the start of a program. It's constructor is calling `system("mkdir #Output_*** ")` where " *** " are `DataIO::date`. Ofc this is current date expressed in seconds. And the point to all of this is to create unique folder for the output. `DataIO::DataIO()` constructor is just the function in witch i can do all of it, at the start of a program without calling it explicitly. – PatrykB Mar 17 '17 at 22:46
  • One more thing c++ is missing: static constructors. Oh well. Maybe in future standards? ;) – Jerfov2 Mar 18 '17 at 19:02

0 Answers0