http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

TheGrumpyProgrammer



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gameprogrammer] Re: problem with singleton



El mié, 16-06-2004 a las 00:16, Dave Slutzkin escribió:

> Although...  There is a slight problem with implementing Singleton this
> way, which you may or may not run into.  (If you don't, don't worry
> about it.)  This static object has to be initialized at some point, and
> the compiler will generate the code to initialize it at a random time
> (or at least, one which you have no control over).  If you try to access
> it before it's initialized (like from the constructor of another static
> object), you run into problems because it's not created yet.  So there
> are multiple other and probably better ways to do it, as mentioned in
> detail in Alexandrescu's Modern C++ Design book.  This is a big, complex
> book though.
> 
> Briefly, the other way I usually do it is:
> 
> static TVTextures::singleton()
> {
>   static TVTextures sSingleton;
>   return sSingleton;
> }
> 
> This ensures that as soon as the function is called, this object is
> allocated so it always exists when it's used.
> 
What would be the best way to implement it but using a pointer for the
instance?

-- 
Roger D. Vargas
Linux User: 180787
ICQ: 117641572
jabber.ehtsc.co.cu: roger
"Object-oriented programming is an exceptionally bad idea which could
only have originated in California." 
- E.W. Dijkstra



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html