Initialising library

Started by
1 comment, last by Aardvajk 12 years, 6 months ago
I have an Application class, and it uses the PHYSFS library, which requires that the functions PHYSFS_init() and PHYSFS_deinit() be called to set up the library and then clean it up.

In my Application.cpp I simply have this:


class PHYSFS
{
public:
PHYSFS()
{
PHYSFS_init(NULL);
}
~PHYSFS()
{
PHYSFS_deinit();
}
};

PHYSFS physfs;


Is this a good idea?
Advertisement
That's fine as long as you can avoid being bitten by the "static initialization order fiasco".

I.e. don't use the same technique for anything that depends on any of the other static constructors also having been run.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
May be a better idea to have an object that represents the life of your application, and have the PHSYFS instance be a member of it. I tend to have an Application class that is created as a local variable in WinMain and have objects that live for the entire application be members of that.

This makes it very clear and explicit exactly what order things are created and destroyed, and plays nice with exceptions as well.

This topic is closed to new replies.

Advertisement