This topic has been archived. It cannot be replied.
-
工作学习 / 专业技术讨论 / Singleton questions?
-simali(随笔);
2006-1-26
{638}
(#2746078@0)
-
when is m_inst constructed in the second example?
-ice(brainteaser);
2006-1-26
(#2746108@0)
-
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.
-simali(随笔);
2006-1-27
(#2750208@0)
-
no, the first one is not a real Singleton, every time you will get a different object.
-luxe(luxe);
2006-1-27
(#2750285@0)
-
come on, it's a static variable!
-simali(随笔);
2006-1-28
(#2751303@0)
-
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.
-guestagain(guest again);
2006-1-28
{229}
(#2751401@0)
-
The first one should work. The static variable in a funtion is allocated memory only the first time the funtion is called.
-benbenli(大岭);
2006-1-29
(#2751428@0)
-
I think you are right. hehe, it's time to recall a lot of things.
-guestagain(guest again);
2006-1-29
(#2751431@0)
-
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.
-benbenli(大岭);
2006-1-29
{498}
(#2751407@0)
-
you can alway use constructor, why you will lost control?
-guestagain(guest again);
2006-1-29
(#2751417@0)
-
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.
-benbenli(大岭);
2006-1-29
(#2751420@0)
-
you can catch it in the constructor, and change a flag.
-guestagain(guest again);
2006-1-29
(#2751422@0)
-
Yes, you can check the flag in main() later. If we avoid globals and class static objects, we can control it better.
-benbenli(大岭);
2006-1-29
(#2751427@0)
-
for a Singleton, I don't think we can avoid class static objects.
-guestagain(guest again);
2006-1-29
(#2751429@0)
-
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.
-benbenli(大岭);
2006-1-29
(#2751430@0)
-
thanks for your correction. :)
-guestagain(guest again);
2006-1-29
(#2751433@0)
-
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!
-benbenli(大岭);
2006-1-29
(#2751435@0)
-
same to you.
-guestagain(guest again);
2006-1-29
(#2751441@0)
-
but you are right, the m_inst should not be initialized like that.
-guestagain(guest again);
2006-1-29
(#2751426@0)
-
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
-ice(Coding Monkey);
2006-1-29
(#2751812@0)
-
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?
-simali(随笔);
2006-1-31
(#2756065@0)
-
up
-simali(随笔);
2006-2-7
(#2769754@0)
-
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
}
-spaspa(小屁吖);
2006-2-3
{350}
(#2761112@0)
-
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?
-simali(随笔);
2006-2-4
{195}
(#2762712@0)
-
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.
-spaspa(小屁吖);
2006-2-8
{189}
(#2770616@0)
-
sorry, created it in heap after main.
-spaspa(小屁吖);
2006-2-8
(#2770619@0)
-
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
-simali(随笔);
2006-2-8
(#2772173@0)