Jump to content
  • Advertisement
Sign in to follow this  
chawk

Singleton objects or static functions/data?

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Recently, I've been working on yet another version of my basecode/game-engine code/etc. As far as singletons go, I've never really paid much attention to them, but I had stumbled across an article after browsing these forums a bit and I was intrigued. To get right to the point, I'm torn between sticking with my current method of implementing "singletons", which is essentially a class with all static data and all static functions, and actual singletons. I never instantiate the object and all calls to functions, accessors, etc, are things like Engine::loadThis() and Window::setThat(). After looking over a few simple true singleton implementations, I can't decide on whether the pros outweigh the cons or not. I see it as such:
// static version example
int main()
{
    Engine::init();
    Engine::doStuff();
    Engine::shutdown();
}

// or singleton, with a global pointer
Engine* e;

int main()
{
    e = Engine::getInstance();
    e->init();
    e->doStuff();
    e->shutdown();
}

It's a little less typing using the global pointer, since I'm not constantly writing Engine:: or Window:: or FileSys::, but is there an advantage under the hood over what I'm doing currently? My initial thoughts make me feel like constantly accessing pointer to call functions (__thiscall) is extra machine code, but with compiler optimizing, this may not be an issue. Any thoughts on the good, the bad, and the ugly? What do you guys use? I'm stuck.

Share this post


Link to post
Share on other sites
Advertisement
It's essentially doing the same thing, so I think that it's a matter of taste in which method you'd go for. Personally, I'm prone to use the MyClass::getSingleton() method because I don't like typing :: in excess, and it just generally feels cleaner (interpret that how you will). ;)

Share this post


Link to post
Share on other sites
First of all, i would drop the global pointer.

/* Synatic Sugar */
Object* instance = Object::getInstance();

instance->setFoo(1);
int i = instance->getBar();



Having a global pointer to a singleton makes no sense to me, the point of the singleton pattern is to have safe access to a global instance of an object.
(e.g, scenario)

class Manager : public Singleton<Manager> {
public:
void registerObject(Object* obj);
void unregisterObject(Object* obj);

protected:
Stack<Object> objects;

};


/* The dark side */
Manager* instance;
class Object {
public:
Object() {
/* You have no guarantee that instance is infact valid, shure you could check it, but again then there is no point in using a singleton. */
instance->registerObject(this);
}
};

/* And now for something slightly different. */
class Object {
public:
Object() {
Manager* instance = Manager::getInstance();
instance->registerObject(this);
}
};



uhm and thats all that comes to mind right now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!