×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Singleton questions?

I have 2 singleton classes as following, any body can tell me what's the difference????

class Singleton {
public:
static Singleton& Instance()
{
static Singleton m_inst;
return m_inst;
}
private:
Singleton ();
~Singleton ();
}

class Singleton {
public:
static Singleton& Instance()
{
return m_inst;
}
private:
Singleton ();
~Singleton ();

static Singleton m_inst;
}

In first class, the local static variable is declared inside the function; in second one, it is declared inside the class.
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / Singleton questions?
    I have 2 singleton classes as following, any body can tell me what's the difference????

    class Singleton {
    public:
    static Singleton& Instance()
    {
    static Singleton m_inst;
    return m_inst;
    }
    private:
    Singleton ();
    ~Singleton ();
    }

    class Singleton {
    public:
    static Singleton& Instance()
    {
    return m_inst;
    }
    private:
    Singleton ();
    ~Singleton ();

    static Singleton m_inst;
    }

    In first class, the local static variable is declared inside the function; in second one, it is declared inside the class.
    • when is m_inst constructed in the second example?
      • In the second example, m_inst is constructed at the time of loading the program; while in first one, it is constructed at the time when process pass the function. that's what i think at this time.
        • no, the first one is not a real Singleton, every time you will get a different object.
          • come on, it's a static variable!
            • I think luxe is right.
              In first case, m_inst is a function variable, not class variable, every time you create a new Instance, It will create a new object., although it is static. Do a simple test(give the class a none static property) , you will know.
              • The first one should work. The static variable in a funtion is allocated memory only the first time the funtion is called.
                • I think you are right. hehe, it's time to recall a lot of things.
        • The second one should be avoided.
          The second one the object is instantiated when the class is loaded. That happens before function main is called. Then from your code, you have no control to it. If there is any thing wrong when instantiating the singleton, you may not have a chance to write log because the log file path has not been read from configuration yet. You can not rely on a static logging class to solve this issue because the sequence of instantiating static objects is not defined. So the second one should be avoided.
          • you can alway use constructor, why you will lost control?
            • What if an exception is thrown out from a c-tor of static object? You can not catch it because main() is not called yet.
              • you can catch it in the constructor, and change a flag.
                • Yes, you can check the flag in main() later. If we avoid globals and class static objects, we can control it better.
                  • for a Singleton, I don't think we can avoid class static objects.
                    • The first example does. If you prefer class static variables than funtion static variables, you can use pointers instead of objects. Then you need to add synchronization for thread-safety.
                      • thanks for your correction. :)
                        • you are welcome. it's always good to discuss and share. It's time to go to bed now. Good night and Happy Chinese New Year!
                          • same to you.
              • but you are right, the m_inst should not be initialized like that.
        • i think i saw a lecture note at some point about the similar question. one of the comments is that the second way was wrong. I am not falimilar with your question enough, just want to throw out some of my thoughts
      • thank you guys, this is great and it makes things clear. global static object is constructed at loading time, so it's better to avoid. but, does the first one thread-safe?
        • up
    • i think this is the best, use pointer , code is inside.
      class Singleton
      {
      public:
      static Singleton* Instance()
      {
      //use critical section to proect this method
      //if this is called in different threads
      if(m_pInst == null)
      m_pInst = new Singleton;
      return m_pInst;
      }
      private:
      Singleton ();
      ~Singleton ();

      static Singleton* m_pInst; //m_pInst must be initialized in cpp file
      }
      • this is a basic solution. it's hard to say it's the best one.
        the main problem with this is that you need to delete the object explicitly if you don't need it anymore.

        and one potential issue is, what if you mistakely delete it, and then try to use it?
        • you maybe right? but in some cases, this is the best. I encounter some problem with creating a global class instance, in the constructor of that class, i created a critical section,
          and when i debug the program, the system(xp) hang up. If i create the object in stack after main, it's fine.

          and another problem is some object cannot be created before main or crashes.
        • sorry, created it in heap after main.
          • haha, that never happens to me. by the way, it uses static local variable, not global class. static local variable only get initialized when you call the function