mercredi 31 août 2016

initialization order of static const initialized with functions

In short, I need to initialize a const static member with some value obtained from a file and keep it thereby same for every object derived from it.

So let's say I've a program -

#include <fstream>
#include <iostream>
#include <string>

class A
{
public:
static const int VAL1;
static const int VAL2;
};

int F(const std::string);

const int A::VAL1 = F("1.txt");
const int A::VAL2 = F("2.txt");

int F(const std::string filename)
{
  std::ifstream file(filename);
  int result = 0;
  file >> result;
  return result;
}

int main () {
  std::cout << A::VAL1 << " " << A::VAL2 << "\n";
}

Is it guaranteed that static members will be always initialized before creation of objects, because that's what I want :/

Aucun commentaire:

Enregistrer un commentaire