Quote:Original post by bubu LV
You also can (but should not!) change initialization order with init_priority attribute (GCC only).
And if you want the same thing with MS's compiler, then there's init_seg.
Quote:Original post by bubu LV
You also can (but should not!) change initialization order with init_priority attribute (GCC only).
Quote:Original post by TheFlyingDutchman
This works perfectly for me, no need for global variables this way and I am still able to destruct the Window instance in the way I want. Example usage:
*** Source Snippet Removed ***
int main(int argc, char **argv){ try { Window window; Game game(window); game.run(); } catch (std::exception & e) { cerr << "Error: `" << e.what() << "'." << endl; return 1; } return 0;}
Quote:Original post by AntheusQuote:Original post by TheFlyingDutchman
This works perfectly for me, no need for global variables this way and I am still able to destruct the Window instance in the way I want. Example usage:
*** Source Snippet Removed ***int main(int argc, char **argv){ try { Window window; Game game(window); game.run(); } catch (std::exception & e) { cerr << "Error: `" << e.what() << "'." << endl; return 1; } return 0;}
No cleanup needed, systems are clearly inter-connected during construction, and you're guaranteed proper de-initialization order.
Your approach is fine - but it's idiomatic for C. C++ offers implicit construction/destruction. The above style does exactly the same, yet if applied throughout the project, results in simpler and more robust code, that does not suffer from undefined behavior.
And most importantly, every C++ class has initialize(), destroy() and instance() functions already. They're constructor, destructor and instance respectively.
Allocating something statically brings along many issues which are very rarely required.
Quote:Original post by DevFredQuote:Original post by TheFlyingDutchman
I am using this singleton class setup for almost any class [...] that I need only one instance for
NEEDING one instance and NOT ALLOWING MORE THAN one instance is not the same. You are abusing the singleton pattern.
Quote:Original post by dmail
I suspected this would turn into another singleton thread after TheFlyingDutchman's post, might as well close it now :)
Quote:Original post by TheFlyingDutchman
Well, I think the examples I gave are pretty good examples of classes you only want one instance: Window, Game, Libraries. Typical classes that need only one instance (and why allow more of them? If my design is such that there is a single GlslLibrary, then my design should force that there is only one such library).