I do not know how to implement a Singleton Class

Started by
33 comments, last by jag_oes 22 years, 1 month ago
quote:Original post by Solo
Hmm... I actually started using the technique I described to wrap certain DirectX objects (I actually ended up making a sort of smart pointer/singleton hybrid). Anyway, I always figured that somewhere someone else must have thought of the same technique and documented it, but now I''m not so sure. Perhaps I''ll document the technique in an article. Might make an interesting sweet snippet. Any criticism of the technique not already posted would be greatly appreciated.


A singleton is a singleton, a smart pointer is a smart pointer.

A singleton should not need to worry about copying issues. A smart pointer should never need to worry about access to a single instance. You can use the singleton class to hold a smart pointer that holds a DirectX pointer.
Advertisement
quote:Original post by Shrew
This method isnt ''pure'' you need to:
new CMyClass;
CMyClass* const pSingleton = CMyClass::Instance();
delete pSingleton;


That would be not exception safe.
quote:Original post by Void
Original post by Shrew
This method isnt ''pure'' you need to:
new CMyClass;
CMyClass* const pSingleton = CMyClass::Instance();
delete pSingleton;


That would be not exception safe.

currently it isn''t - after I posted up my source I was thinking about it and started to edit my post but never got to finishing it.

Be aware that I''m not using exceptions - possibly a very bad move but my ''anecdotal'' evidence suggests that adding it now would be as awkward as trying to add const-qualification to a large project. Which is a shame. And I''m aware that what I''ve written only copies most of the behaviour of a singleton; by their very nature singletons tend to preclude new and delete by the client. Such are the compromises we make

But… as a real point - could it not be made exception safe?

Robert Swan
My company - Aah Games - http://www.aahgames.com
Yes, making code exception safe involves a large structural change. Highly unrecommended for ongoing projects with a pounding dateline.

I feel I should write an article on singleton. Wait for it.
quote:Original post by Void
Yes, making code exception safe involves a large structural change. Highly unrecommended for ongoing projects with a pounding dateline.

I feel I should write an article on singleton. Wait for it.


There are already many articles about this on the internet.

This topic is closed to new replies.

Advertisement