Skip to main content
gamedev.net
🔒 Locked

Singleton objects or static functions/data?

Started by chawk 1 reply 850+ views
Original Post
chawk
chawk
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.
HopeDagger
HopeDagger
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). ;)
decsonic
decsonic
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.