Jump to content


Safe Singleton (C++)


7 replies to this topic

#1 Almos

    Member

  • Members
  • PipPip
  • 99 posts

Posted 22 April 2010 - 08:12 AM

Hello, sorry for bothering.

While browsing the net in search for a safe, easily deallocable singleton I've come upon the solution I'm posting below. One nice thing about it is that the singleton instance is not allocated on the heap but instead declared as a static variable inside of the instance method, which guarantees both the automatic initialization when the method is first called and easy cleanup once the program terminates. Also, the copy constructor and the "=" operator are both declared as private and overloaded so as to prevent the instance to be copied, but that should be obvious. Decided to share.

I hope it's not a repost.


class cSingleton {

    public:

    static cSingleton* instance() {

        static cSingleton ptr;

        return &ptr;

    }



    private:

    ~cSingleton() { }

    cSingleton() { }

    cSingleton(const cObjectManager& inst) {}

    void operator=(const cSingleton&) const {}

};


I want to make a game as good as Elder Scrolls oblivion with no programming, just point&click. If it's not possible, I want a team of programmers I'd be able to order around. After all, I'm a n00b.

#2 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 22 April 2010 - 08:45 AM

Looks like a good implementation.

Still, I hope you are familiar with the problems of singletons (order of destruction), and the alternatives.

In my own development I have found that a monostate (static methods/variables only) class is almost always a better solution than a singleton.

#3 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 22 April 2010 - 11:20 AM

This is indeed the standard C++ way for singletons, but note that the solution is not thread-safe.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#4 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 22 April 2010 - 01:53 PM

I have never found any practical use for singletons. Probably the closest altertative I use is activation of a class instance globally and accessing the instance through a global accessor. Upon destruction of an instance check if it's active and deactivate if so.

#5 appleGuy

    New Member

  • Members
  • PipPip
  • 25 posts

Posted 23 April 2010 - 08:04 AM

JarkkoL said:

I have never found any practical use for singletons. Probably the closest altertative I use is activation of a class instance globally and accessing the instance through a global accessor. Upon destruction of an instance check if it's active and deactivate if so.

Factory Design Patterns are useful. They are often a singleton as you normally only want one factory, with global availability, through a single access point.

#6 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 23 April 2010 - 11:13 AM

I use factory pattern as well, but I haven't had any use of making it a singleton. Globally accessible yes, but not a singleton.

#7 Almos

    Member

  • Members
  • PipPip
  • 99 posts

Posted 23 April 2010 - 12:49 PM

@geon: I didn't know about monostates until your post; looked them over and they indeed look like a safe alternative to singletons.

My own predilection towards the singleton pattern stemmed from the fact that Ogre makes extensive use of it.
I want to make a game as good as Elder Scrolls oblivion with no programming, just point&click. If it's not possible, I want a team of programmers I'd be able to order around. After all, I'm a n00b.

#8 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 23 April 2010 - 12:56 PM

Well, what I have heard, OGRE isn't exactly a prime example of good software engineering practices and is often referred as being over-engineered with excessive inheritance hierarchies, etc.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users