0

So I have a class with static variables, the reason they are static is for (while it may seem insignificant) efficiency (only being required to load once, reduce redundancy of storage in memory of the same file).

Anyway what I'd like to know, is there a way to check if a variable has been loaded?

Or is there a way to have a specific constructor called the first time an instance of this class is created and another used while other instances exist?

If neither of these are appropriate what is the solution?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
CS Dude
  • 421
  • 4
  • 14
  • 3
    Static member variables are constructed and initialized before your `main` function is called. If `main` has been called, then the static member variables have been "loaded". – Some programmer dude Sep 26 '15 at 13:34
  • 1
    There is a SingleTon Design Pattern for Efficient Code Design,please go through this:: https://sourcemaking.com/design_patterns/singleton/cpp/1 – Nishant Sep 26 '15 at 13:46
  • @Nishant_b9: ...and then discard it as a terrible idea and forget you ever read about it. – Lightness Races in Orbit Sep 26 '15 at 14:00
  • Thanks for the responses, @Joachim Pileborg I didn't know static member variables had to be set before `main` was called, I was initalizing them in my .cpp without values. @Nishant I know the singelton pattern but I didn't feel it was applicable here as I still wanted the resources to be alterable between different instances if required. – CS Dude Sep 26 '15 at 21:47

3 Answers3

3

If your static members are private, and initialized in the same translation unit as all of your class's member functions, then the standard guarantees that the static members will be initialized before they are used. See: When are static C++ class members initialized?

There are other situations where this guarantee does not help you (e.g. accessing non-private static members from another translation unit, or from inline member functions).

You can play games with isInitialized flags, but be aware that without further work this is not thread-safe.

The C++ FAQ recommends to wrap static class instances in functions, this ensures that they are initialized on first use. e.g.:

Fred& x()
{
    static Fred* ans = new Fred();
    return *ans;
}

Source: https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use

Community
  • 1
  • 1
Ross Bencina
  • 3,822
  • 1
  • 19
  • 33
0

Here you go:

class Test {
  static bool isInitialized;
public:
  Test() {
    if (!isInitialized) {
      // do whatever you need here
      // ...
      isInitialized = true;
    }
  }
};

bool Test::isInitialized = false;
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

You may do something like:

struct StaticData
{
  // some variables
};

class YourClass
{
public:
    YourClass(/*..*/) {
        if (staticData == nullptr) {
            staticData = std::make_unique<StaticData>(/*..*/)
        }
    }
private:
    static std::unique_ptr<StaticData> staticData;
};

static std::unique_ptr<StaticData> YourClass::staticData;
Jarod42
  • 203,559
  • 14
  • 181
  • 302