开发者

Singleton Class Implementation Problem

There is a static pointer aSingletonClass *instance_ and a static function void deleteInstance( void ) declared in the Code 1.

I implemented the Code 2 without using a static pointer and static function(i thought Singleton can be achieved without it but I am not sure!!!)

Please advice me whether this change can be hazardous and in which case my program can crash.

Code 1:

class aSingletonClass
{
public:

  static aSingletonClass *getInstance( void )
  {

    if(!instance_)
      instance_ = new aSingletonClass;
    return instance_;
  }

  static void deleteInstance()
  {
    if(instance_)
      delete instance_;
    instance_ = NULL; //important as this can create dead reference problems
  }
开发者_C百科
private:

  static aSingletonClass *instance_;

  aSingletonClass() {};

  ~aSingletonClass() {};
};

int main()
{
  aSingletonClass *someVar = NULL;

  someVar = aSingletonClass::getInstance();

  aSingletonClass::deleteInstance();

  return 0;
}

Code 2:

class A
{
    int x;
    A(int a)
    { x=a;  }

    ~A()
    { }

public:
     static A* start()
     {
       A* ptr1 = new A(3);
       return (ptr1);
     }


     void end()
     {
       delete this;
     }
};

int main()
{
  A* ptr=A::start();

  ptr->end();
}


Your second version is not a singleton because you have no static A* pointer and just return a new one whenever start() is called, so the following is valid in your code:

int main()
{
    A* ptr = A::start();
    A* ptr2 = A::start();
    A* ptr3 = A::start();
    // etc
    ptr->end();
    ptr2->end();
    ptr3->end();
    return 0;
}; // eo main


Ugh. Not a nice way to do singletons!

I would do something like the following..

class A
{
private:
  A() {}

public:

  static A& instance()
  {
    static A _inst;
    return _inst;
  }

};

To use,

A& inst = A::instance()

Will always return the single instance!

EDIT: This approach has several advantages - you don't have to worry about cleaning up but it's lazy loaded (or initialized), and you work with references! Heck you could even have a constant singleton (why, I'm sure you can think of some use! ;) )


Code 2 will not be a singleton. In start() function, you return a pointer to a new A object. But, in the end() function, you delete this object. So, it will not work.


Your code should be like this:

class A    {
  public:
    static A *getInstance( void )
    {
       if(!instance_)
          instance_ = new A();
       return instance_;
    }
    static void deleteInstance()
    {
        if(instance_)
           delete instance_;
        instance_ = NULL; //important as this can create dead reference problems
    }
    static A* start()
    {
        A* ptr1 = A::getInstance();
        ptr1 -> started = true; // check here if A had been started already !!!!
        return (ptr1);
    }

    static void end()
    {
        A* ptr1 = A::getInstance();
        ptr1 -> started = false; // check here if A had ever been started !!!!
        //A::deleteInstance();      // may be it will be useful here
    }
  private:
    bool started = false;
    static A *instance_;
    A() {};
    ~A() {};
};

int main()
{
  A *someVar = NULL;
  someVar = aSingletonClass::getInstance();
  A::deleteInstance();

  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜