Possible Duplicate:
A Singleton that is not globally accessible
Do you know a good design pattern that would make sure that only one instance of an object is created without making this object global in c++?
That’s what singleton do but I really need it NOT to be global for code access security reasons.
Thanks for your help!
I think you want something like this (note: copy-pasted from an answer I already wrote and forgot about):
Now you get an exception if the class is constructed more than once:
There’s no way to enforce the single-instance policy at compile-time in C++, though.
(Also good on you for noticing that singletons are dumb. Globally accessible and singly-creatable are two different concepts, and should only be combined by coincidence, not by design.)