pingu
pingu

Reputation: 8827

Persisting global data that needs initialising in C++

I have a global user defined type "foo"in a dll that is responible for the creation and deletion of a reference counted HINSTANCE. Problem is it needs to be initialised with a string by a function called on the dll.

What is my best option for doing this? How can a function create a "foo" that will be global and persist with a valid HINSTANCE over multiple function calls. Thanks

Upvotes: 0

Views: 126

Answers (1)

user707582
user707582

Reputation:

You can use a singleton:

class CFoo
{
public:
  static CFoo* m_instance;
  static CFoo* GetInstance()
  {
      if(!m_instance)
      {
          m_instance = new CFoo();
      }
      return m_instance;
  }
private:
  CFoo();
};

Upvotes: 1

Related Questions